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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.