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

I need to automate the date by only having the user to hard code the year and month. The below program works perfectly fine for the month of 10, 11 and 12 but did not work for month from 1 to 9. I realized this is due to single digit of month instead of double digit. Could anyone help on this?

 

Be noted I use this method to atuomate the date is because in the later of the program, there will be SQL pass through between other databases, so I have to automate the date in both of date9. (‘01nov2019’d) And yymmdd10 (2019-11-01) format.

 

%LET YYYY = 2019; /* year*/

%LET MM = 11; /*month*/

DATA _NULL_;

CURRENT_MONTH = input(cats(&YYYY,&MM,"01"),yymmdd8.) ;

SASD1 = quote(put(CURRENT_MONTH,date9.),"'")||"D" ;

SASD2 = quote(put(intnx('MONTH',CURRENT_MONTH,0 ,"E"),date9.),"'")||"D" ;

SQLD1 = quote(put(CURRENT_MONTH,yymmdd10.),"'") ;

SQLD2 = quote(put(intnx('MONTH',CURRENT_MONTH,0 ,"E"),yymmdd10.),"'") ;

CALL SYMPUT('SASD1',SASD1);

CALL SYMPUT('SASD2',SASD2);

CALL SYMPUT('SQLD1',SQLD1);

CALL SYMPUT('SQLD2',SQLD2);

RUN;

%PUT &SASD1 &SASD2 &SQLD1 &SQLD2;

 

%PUT &SASD1 &SASD2 &SQLD1 &SQLD2;

'01NOV2019'D

'30NOV2019'D

 

'2019-11-01'

'2019-11-30'

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @LL5   I wonder how your code worked perfectly fine without quotes around macro variable references within CATS? Well of course CATS does an autoconverson to char with best format, so &yyyy or &mm didn't impact. However the numeric resolution is a problem as 01 would become 1. 

CURRENT_MONTH = input(cats(&YYYY,&MM,"01"),yymmdd8.) ;

?

 

So, ideally the below should address the issue

 

CURRENT_MONTH = input(cats("&YYYY","&MM","01"),yymmdd8.) ;

So, with this change,your single digit month value can be assigned into your variables like the below

 


%LET YYYY = 2019; /* year*/

%LET MM = 01; /*month*/

 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Hi @LL5   I wonder how your code worked perfectly fine without quotes around macro variable references within CATS? Well of course CATS does an autoconverson to char with best format, so &yyyy or &mm didn't impact. However the numeric resolution is a problem as 01 would become 1. 

CURRENT_MONTH = input(cats(&YYYY,&MM,"01"),yymmdd8.) ;

?

 

So, ideally the below should address the issue

 

CURRENT_MONTH = input(cats("&YYYY","&MM","01"),yymmdd8.) ;

So, with this change,your single digit month value can be assigned into your variables like the below

 


%LET YYYY = 2019; /* year*/

%LET MM = 01; /*month*/

 

LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

Thank you novinosrin, I did not pay attention to the quotes and now with your solution it works well. Thanks again.

PaigeMiller
Diamond | Level 26

You ought to stop working with calendar dates as text strings. This causes no end of problems. YOu need to work with  calendar dates as SAS dates, and then format them to look properly to humans.

 

%LET YYYY = 2019; /* year*/
%LET MM = 11; /*month*/
DATA _NULL_;
    CURRENT_MONTH = mdy(&mm,1,&yyyy);

Now all your PUT() function calls are unnecessary, and appending a "d" on the end of the string is unnecessary, there is no need to put quotes around anything (and so you can't mess that up) and the result is much cleaner and much more efficient and readable code.

--
Paige Miller
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

Thanks PaigeMiller. This method works well down the road. Also thanks for letting me know D is not necessary after the date.

Tom
Super User Tom
Super User

You should probably just use the MDY() function to convert your year and month to a date. Since they take numeric inputs leading zeros are not needed.

CURRENT_MONTH = mdy(&YYYY,&MM,1) ;
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

Thanks Tom. This approach worked well.

 

%LET YYYY = 2020; /* year*/

%LET MM = 01; /*month*/

DATA _NULL_;

CURRENT_MONTH1 = mdy(&mm,1,&yyyy);

 

D1 = CURRENT_MONTH1;

D2 = intnx('MONTH',CURRENT_MONTH1 ,0 ,"E");

CALL SYMPUT('D1',D1);

CALL SYMPUT('D2',D2);

RUN;

 

 

DATA TEST;

A = PUT(&D1, DATE9.);

B = PUT(&D2, DATE9.);

C = PUT(&D1, YYMMDD10.);

D = PUT(&D2, YYMMDD10.);

RUN;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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