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?
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.
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.
This question is very close to one of your similar questions from 3 years ago.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.