Concatenate date with character variable ,SAS - sas

I'm facing an issue where I cannot concatenate a date formatted variable with a character variable. The date format is 'date9.'. What happens is that the date turns into numbers when the concatenation is done.
Example:
Agency: RCL
Date: 24MAR2008
Result: RCL17615
It should be: RCL24MAR2008
This is my code:
data work.collectionrate_new;
set work.collectionrate;
tran_id=compress(agency||date);
run;
Thank you.

you just need a put(date,date9.) to format your date as character:
Code:
data new;
format date date9. ;
date="24MAR2008"d;
agency="RCL";
tran_id=cats(agency,put(date,date9.));
put _all_;
run;
Output:
date=24MAR2008 agency=RCL tran_id=RCL24MAR2008

Related

Converting a date string with leading year into a data in SAS

I've had various success with the input statement converting character strings into dates, but when the date is formatted YY/MM/DD, I can't find the right INFORMAT to get INPUT to convert it to a number.
e.g.
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDDw.);
date = date_no;
run;
I've tried using the list of SAS INFORMATs but non of them seem to work when converting this type of date. I can only get INPUT to work with formats like DDMMYY10.
Expected output would be:
date_string "22/11/07"
date_no 22956
date 07/11/2022
Code:
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDD8.);
date = date_no;
put date=;
run;
Log:
date=07/11/2022

SAS: Converting numeric to character values

I am trying to convert datatime20. from numeric to character value.
Currently I have numeric values like this: 01Jan200:00:00:00 and I need to convert it to character values and received output like: 2020-01-01 00:00:00.0
What format and informat should be used in aboved ?
I have tried used PUT function to convert numeric to character and tried many option, each time receiving other format. Should be also use DHMS function before PUT ?
There is not a native format that produces that string exactly. But it it not hard to build it in steps using existing formats. Or you could use PICTURE statement in PROC FORMAT to build your own format.
If you don't really care about the time of day part of the datetime value then this is an easy and clearly understand way to convert the numeric variable DT with number of seconds into a new character variable in that style. Use DATEPART() to get the date (number of days) from the datetime value and then use the YYMMDD format to generate the 10 character string for the date and then just append the constant string of the formatted zeros.
length dt_string $21.;
dt_string = put(datepart(dt),yymmdd10.)||' 00:00:00.0';
If you need the time of day part then you could also use the TOD format.
dt_string = put(datepart(dt),yymmdd10.)||put(dt,tod11.1);
Or you could use the format E8601DT21.1 and then change the letter T between the date and time to a space instead.
dt_string = translate(put(dt,E8601DT21.1),' ','T');
If you want to figure out what formats exist for datetime values and what the formatted results look like you could run a little program to pull the formats from the meta data and apply them to a specific datetime value.
data datetime_formats;
length format $50 string $80 ;
set sashelp.vformat;
where fmttype='F';
where also fmtinfo(fmtname,'cat')='datetime';
keep format string fmtname maxw minw maxd ;
format=cats(fmtname,maxw,'.','-L');
string=putn('01Jan2020:01:02:03'dt,format);
run;
A custom format can be defined to return the result of a user defined function. Docs
proc format;
value <format-name> (default=<width>)
other = [<function-name>()]
;
run;
Example:
options cmplib=(sasuser.functions);
proc fcmp outlib=sasuser.functions.temporal;
function E8601DTS (datetime) $21;
return (
translate (putn(datetime,'E8601DT21.1'),' ','T')
);
endsub;
run;
proc format;
value E8601DTS (default=21)
other = [E8601DTS()]
;
run;
data have;
do dt = '01jan2020:0:0'dt to '10jan2020:0:0'dt by '60:00't;
output;
end;
format dt datetime16.;
run;
ods html file='function-based-format.html';
proc print data=have(obs=4); title 'stock E8601DT';
proc print data=have(obs=4); title 'custom E8601DTS';
format dt E8601DTS.;
run;
ods html close;

How to convert string to date in SAS?

I have a table in SAS where in one column, the date is stored (e.g. "2005/10").
How do I have to convert this to a SAS data format?
Among many other tries, I tried this code:
data test;
format date YYMMS.;
date = input(ObservationMonth, YYMMS.);
put date=date9.;
run;
You could just use the anydtdte. informat.
data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;
This informat detects most date formattings and converts the character-stored value to a SAS date.
One way is to use substr() and mdy() to extract the date components:
data _null_;
ObservationMonth ="2005/10";
date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
put date = date9.;
run;
Another option is to use the anydtdte informat (note that results may differ depending on your locale):
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth,anydtdte.);
put date = date9.;
run;
Yet another option is to modify the input to enable use of the YYMMDDw. informat:
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth!!'/01', YYMMDD10.);
put date = date9.;
run;
you're fantastic, guys! Thank you so much, with a "set" statement it works fine!

Create a numeric variable with a date format from Character date variable

I have the Character date variable and I need to convert that into Numeric date Variable. My character date variable is in the format of monyy7. format.
Please suggest me how to convert that into numeric with the format of monyy7.
The desired result can be obtained by using INPUT function:
data a;
input date_char $7.;
cards;
jun2015
apr1914
feb2010
;
run;
data a1;
set a;
format date monyy7.;
date = input(date_char,monyy7.);
run;
you should write it like to_number(to_char(your_date_variable,'dd'),'99').
and example query :
for date select to_number(to_char(sysdate,'dd'),'99') from dual
for month select to_number(to_char(sysdate,'mm'),'99') from dual
for year select to_number(to_char(sysdate,'rrrr'),'9999') from dual

How to input to date format from a number

I'm trying to put a number eg 20141001 into a date9. format eg 01OCT14.
I've tried to use the input function with an input format of yymmddn8. but SAS throws out 'informat could not be found or loaded'
Any ideas how to get around this? (Sample code below)
data _null_;
date=20141001;
output=input(date,yymmddn8.);
format output date9.;
put output=;
run;
You are almost there. Although there is a YYMMDDN format there is not an informat of the same name. Use the YYMMDD informat. The Input function is expecting a character string i.e. the DATE variable. Redefine DATE as a character variable e.g.
data _null_;
date='20141001';
output=input(date,yymmdd8.);
format output date9.;
put output=;
run;
Alternatively you could have used these assignments:
output = input('20141001',yymmdd8.);
or
output = '01oct2014'd;