Hello
I want o get the list of months (in form YYMM) that exists between From_Date and today.
For example:
From_Date is 14MAY2021
Today is 19JAN2022
I need to create a macro var that will contain the values: 2105+2106+2107+2108+2109+2110+2111+2112+2201
%let From_Date='14MAY2021'd;
%let Today=Today();
/**Task: Need to create a new macro var called vector that will contain list of months in form YYMM between these 2 dates**/
You can do it with a macro, like this:
%macro monthlist(from_date,to_date=%sysfunc(today()));
%local month;
%do month=%sysfunc(putn(&from_date,yymmn4.)) %to
%sysfunc(putn(&to_date,yymmn4.));
%if %substr(&month,3)=13 %then
%let month=%eval(&month+88);
%do; &month%end;
%end;
%mend;
%let vector=%monthlist(&from_date);
Or it can be done with a datastep:
data _null_;
date=&from_date;
length list $200;
do while(date<=today());
call catx(' ',list,put(date,yymmn4.));
date=intnx('month',date,1);
end;
call symputx('vector',list);
run;
What have you tried?
I would start with a data step creating one observation for each value. Most likely that data set can be used in the subsequent steps, avoiding a list of values in a macro variable.
You can do it with a macro, like this:
%macro monthlist(from_date,to_date=%sysfunc(today()));
%local month;
%do month=%sysfunc(putn(&from_date,yymmn4.)) %to
%sysfunc(putn(&to_date,yymmn4.));
%if %substr(&month,3)=13 %then
%let month=%eval(&month+88);
%do; &month%end;
%end;
%mend;
%let vector=%monthlist(&from_date);
Or it can be done with a datastep:
data _null_;
date=&from_date;
length list $200;
do while(date<=today());
call catx(' ',list,put(date,yymmn4.));
date=intnx('month',date,1);
end;
call symputx('vector',list);
run;
%let From_Date='14MAY2021'd; %let Today=Today(); data _null_; length list $ 200; do date=&From_Date to &Today ; if month(date) ne month then list=catx('+',list,put(date,yymmn4.)); month=month(date); end; call symputx('list',list); run; %put &=list ;
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.