0
votes

I am struggling with this compress function in the code below that I am trying to convert.

Old code: (This code works and returns the results below)

data _null_;
%let startdt='2015/11/1'; 
date_num=compress(&startdt,"'");
call symputx('date_num',date_num);
%put &startdt;
%put &date_num;
run;

This code returns values for the macro variable startdt as 2015/11/1 and datenum as 2015/11/1.

I am trying to achieve similar functionality using macro variables for dates.

New code: (This code gives me an error and I am not able to figure out why)

data _null_;
dt = date();
last_mth_beg = intnx('month',dt,-1,'beginning');
call symput('startdt',put(last_mth_beg,YYMMDDS10.));
date_num=compress(&startdt,"'");
call symputx('date_num',date_num);
%put &startdt;
%put &date_num;
run;

I am getting an error when I run this new code. I would like to get results as in the old code. Please help. Thank you!

2
Move your %LET and %PUT statements to either before or after your data step, since that is when SAS is going to evaluate them it makes your code easier to read and understand. Leaving them in the middle of the data step makes it look like they are some how being evaluated while the data step is running, which is not true. Does that fix your issue? - Tom
Hi, That does not resolve the issue. The error is in the 'New code' that does not have the let statement. Thanks! - PJay
Your second data step should also run fine, but you do need to move the %PUT statements to after the run; statement. Otherwise they will generate an error that the macro variables don't exist or, perhaps worse, show you the values they had BEFORE the data step ran. - Tom

2 Answers

0
votes

Your first data step is just removing the quotes from your macro variable. You can do this in macro code by either using %sysfunc() to call the compress() function. Or even just use the %scan() function.

%let date_num = %scan(&startdt,1,%str(%'));

In the second one it looks like you are trying to use INTNX() function on the date value. But your macro variables do not contain date values. If you want to treat '2015/11/1' as if it was a date then you will need to use an input function to convert it first.

%let last_mth_beg = %sysfunc(intnx(month,%sysfunc(inputn(&date_num,yymmdd10)),-1,b),yymmdd10);
0
votes

I am not sure why your old code is not giving you an error. The first time you run it, you should see the error,

WARNING: Apparent symbolic reference DATE_NUM not resolved.

Then, after the data step has run, &DATE_NUM will have a value. You cannot use %PUT inside a data step to display a macro variable you are creating within the data step.

In your second set of code, the value of the STARTDT macro variable you are creating does not have single quotes around it in the first place. If you just run the following, you will get the correct result:

data _null_;
dt = date();
last_mth_beg = intnx('month',dt,-1,'beginning');
call symput('startdt',put(last_mth_beg,YYMMDDS10.));
run;

%put &startdt;

When I run it, I see

2016/11/01