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 help to 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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1196 views
  • 2 likes
  • 4 in conversation