BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

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
s_lassen
Meteorite | Level 14

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;

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

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.

 

s_lassen
Meteorite | Level 14

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;
Ksharp
Super User
%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 ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2121 views
  • 4 likes
  • 4 in conversation