Hello
User define a macro variable called YYMM (Represent Year(2 digits) and month), for example: 2306 represent year 2023 and month 06.
The target is to calculate 2 other macro variables:
Macro1-End of month date (in this example the macro variable will get value '30JUN2023'd or 23191 because this number represent 30JUN2023)
Macro2-End of month date name without 'd ,in this example : 30JUN2023
(Note: I need it in order to add it to data set name )
What is the way to create macro1 and macro 2 via %let statement?
What is the way to create macro1 and macro 2 via data set?
%let YYMM =2306;
%let mvar_2=%sysfunc(intnx(month,%sysfunc(inputn(&yymm,yymmn4.)),0,e),date9.);
%let mvar_1=%tslit(&mvar_2)d;
data _null_;
sas_dt=intnx('month',input("&yymm",yymmn4.),0,'e');
call symputx('mvar_1',cats("'",put(sas_dt,date9.),"'d"));
call symputx('mvar_2',put(sas_dt,date9.));
run;
Why not just:
%let mvar_1="&mvar_2."d;
?
Bart
I'm not going to do this with %let because given poorly structured stuff, YYMM with 2 digit years, as I believe was brought up to you multiple times when you first started asking questions on this forum, is just plain ugly and the number of %sysfunc nested calls that would be needed to to create this gets moderately hard just to follow the code.
This assumes all of your 2-digit years are 2000 or later. If not good luck and a reason not to use 2-digit years.
I am also including demonstration of YYYYMMNN as for file names that makes much better sense than date9 formated values. YYYYMMNN will sort correctly. Date9 won't even come close. 28Feb (OR 29Feb) then 30Jun (and all the months that end on the 30th) then 31Apr. Garbage to find file names like that especially multiple years worth in the same folder.
%let yymm=2306; data _null_; d =intnx('month',mdy(%substr(&yymm.,3,2),1,20%substr(&yymm.,1,2)),0,'E'); call symputx('eom1',put(d,5. -l)); call symputx('eom2',cats(quote(put(d,date9.)),'d')); call symputx('eom3',put(d,date9.)); call symputx('eom4',put(d,yymmddn8.)); run; %put eom1 = &eom1. eom2= &eom2. eom3= &eom3. eom4= &eom4.;
Do not use 2-digit years.
Do not use 2-digit years.
Do not use 2-digit years.
After Y2K, the use of 2-digit years constitutes the mark of an idiot (and I am being very polite here).
Never use something like the DATE9 format for time-stamping files or other objects. This does not sort chronologically and makes dealing with them harder. Always use dates in YMD order, either with the YYMMDDN8. (suited for SAS datasets) or the YYMMDD10. format.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.