DATA Step, Macro, Functions and more

8. to date9.

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

8. to date9.

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. 


Accepted Solutions
Solution
‎09-13-2017 11:21 PM
Super User
Posts: 6,622

Re: 8. to date9.

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


All Replies
PROC Star
Posts: 1,209

Re: 8. to date9.

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.

 

 

Contributor
Posts: 48

Re: 8. to date9.

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

PROC Star
Posts: 1,209

Re: 8. to date9.

[ Edited ]

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;
Contributor
Posts: 48

Re: 8. to date9.

can I understand it as put(input(put(end_date_id, 8.), yymmdd8.), date9.)
SAS Super FREQ
Posts: 810

Re: 8. to date9.

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.

Solution
‎09-13-2017 11:21 PM
Super User
Posts: 6,622

Re: 8. to date9.

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.

Contributor
Posts: 48

Re: 8. to date9.

Posted in reply to Astounding
Thanks @Astounding & @draycut

@Astounding
This is what I am exactly doing.
Super User
Super User
Posts: 7,929

Re: 8. to date9.

[ Edited ]

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 
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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