BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
R_Chung
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

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.

 

 

R_Chung
Quartz | Level 8

yes it is a numeric, just i want to declare a variable to illustrate it.

PeterClemmensen
Tourmaline | Level 20

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;
R_Chung
Quartz | Level 8
can I understand it as put(input(put(end_date_id, 8.), yymmdd8.), date9.)
BrunoMueller
SAS Super FREQ

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.

Astounding
PROC Star

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.

R_Chung
Quartz | Level 8
Thanks @Astounding & @PeterClemmensen

@Astounding
This is what I am exactly doing.
Tom
Super User Tom
Super User

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 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4062 views
  • 4 likes
  • 5 in conversation