SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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