I have searched many websites and get confused for a numeric date to date9. format.
e.g. 20170101 to 01JAN2017
I have tried many methods:
%let end_date_id = 20170101;
call symputx('end_date_id', input( put( end_date_id, yymmddn8.), date9.));
input( put( end_date_id, 8.), date9.);
end_date_id = input(put(datepart(end_date_id), yymmddn8.),date9.);
put end_date_id = date9.;
call symputx ('end_date_id', input(put(end_date_id, yymmddn8.),date9.));
*Seems this one work;
put(input(put(day_id, 8.),yymmdd8.), date9.)
Is there any sggestion to perform it easily and explain the logic to me.
Thanks a lot.
Just because you put digits into a macro variable doesn't make it numeric. How SAS interprets it depends on the context. For example, this simplified variation would work:
%let end_date_id = 20170101;
Then later:
call symputx('end_date_id', put( input("&end_date_id", yymmdd8.), date9.);
In a DATA step double quotes around the macro variable reference lets it resolve, yet lets the DATA step treat it as a character string. That lets you remove one function from the equation. So the INPUT function converts the string to the proper date value, and the PUT function expresses the result in the proper format.
I'm not sure I understand your question completely. You say that you have a date stored as a numeric variable, but you define end_date_id as a macro variable? Remember that a macro variable is merely text.
yes it is a numeric, just i want to declare a variable to illustrate it.
Ok. Use the INPUT and PUT functions like this
data _null_;
end_date_id = 20170101;
dt=input(put(end_date_id, 8.), yymmdd8.);
put dt date9.;
run;
I suggest to read the doc about date and time values in SAS,
There are also plenty of papers on the topic, for instance How to Read, Write, and Manipulate SAS ® Dates
These informations will give you some background you can build on.
Just because you put digits into a macro variable doesn't make it numeric. How SAS interprets it depends on the context. For example, this simplified variation would work:
%let end_date_id = 20170101;
Then later:
call symputx('end_date_id', put( input("&end_date_id", yymmdd8.), date9.);
In a DATA step double quotes around the macro variable reference lets it resolve, yet lets the DATA step treat it as a character string. That lets you remove one function from the equation. So the INPUT function converts the string to the proper date value, and the PUT function expresses the result in the proper format.
If you want to work just in macro code then you can use %SYSFUNC() to call the INPUTN() function to convert your digits into a date. You can also add a FORMAT to the %SYSFUNC() call to format the string that it returns.
So for example here are ways to convert your digits into an actual SAS date value, a SAS date formatted using the DATE9 format and also a date literal.
%let date_digits = 20170101;
%let actual_date=%sysfunc(inputn(&date_digits,yymmdd8));
%let date9_string=%sysfunc(inputn(&date_digits,yymmdd8),date9);
%let date_literal="%sysfunc(inputn(&date_digits,yymmdd8),date9)"d;
You could use &actual_date or &date_literal in SAS code directly or you could use &date9_string to generate a date literal.
where date=&actual_date
where date=&date_literal
where date="&date9_string"d
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.