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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1518 views
  • 1 like
  • 4 in conversation