Search code examples
sassas-macro

SAS Maco Program Char To Numeric


Hello I am trying to convert the dates in this program into numeric, the program generates dates 12 months as of today. i want the result to look like the first column "date0" 20140731 however i want the date0 column along with every column that is generated to be numeric, is this possible?

%global year0 month0 days; 

%let dt=%sysfunc(today()); 
%let year0=%sysfunc(year(&dt)); 
%let month0=%sysfunc(month(&dt),z2); 
%let period=12; 

%macro setlastday(m,y); 
 %if &m=12 %then %let days=31; 
 %else %if &m=11 %then %let days=30; 
 %else %if &m=10 %then %let days=31; 
 %else %if &m= 09 %then %let days=30; 
 %else %if &m= 08 %then %let days=31; 
 %else %if &m= 07 %then %let days=31; 
 %else %if &m= 06 %then %let days=30; 
 %else %if &m= 05 %then %let days=31; 
 %else %if &m= 04 %then %let days=30; 
 %else %if &m= 03 %then %let days=31; 
 %else %if &m= 02 %then 
 %do; 
 %if %sysfunc(mod(&y,4))=0 
 %then %let days=29; 
 %else %let days=28; 
 %end; 
 %else %if &m= 1 %then %let days=31; 
%mend; 

%macro setdate; 

data TEST; 
 %do i=0 %to . 
 %global t&i /*ds&i*/; 
 %setlastday(&&month&i,&&year&i); 


date&i="&&year&i.&&&month&i.&&days."; 



 call symput("t&i",date&i); 


 call symput("ds&i", 
 "ds&&year&i."||put(&&month&i,z2.)); 


 %let j=%eval(&i+1); 
 %if %eval(&&month&i)=1 %then 
 %do; 
 %let year&j=%eval(&&year&i-1); 
 %let month&j=12; 
 %end; 
 %else 
 %do; 
 %let year&j=%eval(&&year&i); 
 %let month&j=%eval(&&month&i-1); 
 %end; 
 %end; 

run; 
%mend; 

%setdate; 

Solution

  • Macros don't have concept of character /numeric, so you just need to change this:

    date&i="&&year&i.&&&month&i.&&days."; 
    

    and remove the quotes to be 20140731 or whatever, as a numeric.

    If you mean you want it to be a proper SAS date, you can use input, if you really are attached to this convoluted process, or define it as a date constant ("DDMONYY"d, so "31JUL2014"d). Better would be to use SAS date functions in the first place.