🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 01-19-2022 12:22 AM
(3020 views)
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**/
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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 ;