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

Hello

User define  a macro variable with structure YYMM (for example: 2107 means month 7 in year 2021).

From this Macro variable I want to calculate 2 new macro variables: Start and End that will represent start date of month and end date of month.

For example:

%let YYMM=2107;

Macro variable called start  will get value : '01JUL2021'd

Macro variable called end  will get value : '31JUL2021'd 

What is the way to calculate macro variables start and End please?

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Do you just need values you can uses in code?

%let start=%sysfunc(inputn(20&yymm.01,yymmdd8));
%let end=%sysfunc(intnx(month,&start,0,e));

Or do you need values that humans will recognize?

%let start="%sysfunc(inputn(20&yymm.01,yymmdd8),date9)"d;
%let end="%sysfunc(intnx(month,&start,0,e),date9)"d;

Example:

366   %let YYMM=2107;
367   %let start=%sysfunc(inputn(20&yymm.01,yymmdd8));
368   %let end=%sysfunc(intnx(month,&start,0,e));
369   %put &=yymm &=start &=end;
YYMM=2107 START=22462 END=22492
370
371   %let start="%sysfunc(inputn(20&yymm.01,yymmdd8),date9)"d;
372   %let end="%sysfunc(intnx(month,&start,0,e),date9)"d;
373   %put &=yymm &=start &=end;
YYMM=2107 START="01JUL2021"d END="31JUL2021"d

PS Why are you using only 2 digits for year?  I hardcoded the "20" part for the century  This will make sure that future dates like 2050 are not translated as 1950 based on your settings for year cutoff.

View solution in original post

3 REPLIES 3
esjackso
Quartz | Level 8
Is this what you need? It assumes the year has to be in 2000s ... the various put statements are just to show what each step does. Also assumes you actually want the date token in the macro variable

%let YYMM=2107;

data _null_;
format start end mmddyy10.;
year = 20 || substr("&yymm",1,2);
mon = substr("&yymm",3,2);
put year= ;
put mon= ;

start = mdy(mon,1,year);
put start=;

end = intnx('month',start,0,'end');
put end=;

call symputx('startm',"'"||put(start,date9.)||"'d");
call symputx('endm',"'"||put(end,date9.)||"'d");
run;

%put Startm= &startm;
%put endm= &endm;
Tom
Super User Tom
Super User

Do you just need values you can uses in code?

%let start=%sysfunc(inputn(20&yymm.01,yymmdd8));
%let end=%sysfunc(intnx(month,&start,0,e));

Or do you need values that humans will recognize?

%let start="%sysfunc(inputn(20&yymm.01,yymmdd8),date9)"d;
%let end="%sysfunc(intnx(month,&start,0,e),date9)"d;

Example:

366   %let YYMM=2107;
367   %let start=%sysfunc(inputn(20&yymm.01,yymmdd8));
368   %let end=%sysfunc(intnx(month,&start,0,e));
369   %put &=yymm &=start &=end;
YYMM=2107 START=22462 END=22492
370
371   %let start="%sysfunc(inputn(20&yymm.01,yymmdd8),date9)"d;
372   %let end="%sysfunc(intnx(month,&start,0,e),date9)"d;
373   %put &=yymm &=start &=end;
YYMM=2107 START="01JUL2021"d END="31JUL2021"d

PS Why are you using only 2 digits for year?  I hardcoded the "20" part for the century  This will make sure that future dates like 2050 are not translated as 1950 based on your settings for year cutoff.

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
  • 3 replies
  • 897 views
  • 1 like
  • 4 in conversation