BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
StickyRoll
Fluorite | Level 6

How do I convert macro value of MMDDYYYY to work with a SAS Date?

I have a macro variable that stores value in "MMDDYYYY" format. Eg. 07311999.

I want to find a way to convert this 07311999 macro variable to a SAS Date so that it can work in a normal proc sql or data step.

This is my use case.

%let mmddyyyy_var = 07311999;

proc sql;

create table sasuk as

select *

from work.anything

where creation_date = &mmddyyyy_var.;

quit;

** creation_date is a normal sas date column.

As we can already tell from the sampel program above, this will not work as &mmddyyyy_var. is in 07311999 format. How can I convert this into a SAS Date for this sql statement to work?

 

I tried referring to :https://go.documentation.sas.com/doc/en/vdmmlcdc/8.1/ds2ref/n03d6ri46spzeln10bt1iepigfdy.htm

However, those conversion doesn't work for me in this case.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Instead of using a macro value in the mmddyyyy format, I'd suggest using the DATE9 format.  It's just as readable as your format, but is much easier to parse as a date literal value, as in:

 

%let cutdate=31jul1999;
proc sql;
  create table sasuk as
  select *
  from work.anything
  where creation_date = "&cutdate"d ;
quit;

However, if you must use the mmddyyyy format, then:

 

%let cutdate=07311999;
proc sql;
  create table sasuk as
  select *
  from work.anything
  where creation_date = %sysfunc(inputn(&cutdate,mmddyy8.));
quit;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
MarkusWeick
Barite | Level 11

Hi @StickyRoll,

have you tried the INPUT function (Microsoft Word - chap1new.doc (sas.com))?

Best

Markus

 

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
mkeintz
PROC Star

Instead of using a macro value in the mmddyyyy format, I'd suggest using the DATE9 format.  It's just as readable as your format, but is much easier to parse as a date literal value, as in:

 

%let cutdate=31jul1999;
proc sql;
  create table sasuk as
  select *
  from work.anything
  where creation_date = "&cutdate"d ;
quit;

However, if you must use the mmddyyyy format, then:

 

%let cutdate=07311999;
proc sql;
  create table sasuk as
  select *
  from work.anything
  where creation_date = %sysfunc(inputn(&cutdate,mmddyy8.));
quit;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

Maxim 28: Macro Variables Need No Format.

%let mmddyyyy_var = %sysfunc(inputn(07311999,mmddyy8.));

proc sql;
create table sasuk as
select *
from work.anything
where creation_date = &mmddyyyy_var.;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 418 views
  • 2 likes
  • 4 in conversation