budding pro
budding pro

Reputation: 195

write conditional in SAS with DATA _NULL_

I am writing a conditional in SAS starts with DATA NULL

%LET today = today();
DATA _NULL_;
    if day(today) ge 1 and day(today) le 15 then do;
        date1=put(intnx('month',today,-1,'E'), date11.);
        date2=put(intnx('month',today,-1,'L'), date11.);
    end;
    if day(today) > 15 then do;
        date1=put(intnx('month',today,0,'B'), date11.);
        date2=put(intnx('month',today,0,'L'), date11.);
    end;
    call symput('report_date',date1);
    call symput('report_date2',date2);
RUN;

but with above, I am not getting any values for my report_dates.

the condition is:

date 1 = If the current date is greater than or equal to 1 and less than 16, set the date1 to the 16th of the previous month, otherwise set it to the 1st of the current month

date2 = If the current date is 16 and above, set the date2 to the 15th of the current month, otherwise set date2 to the last day of the previous month

Upvotes: 0

Views: 595

Answers (1)

Tom
Tom

Reputation: 51581

The IF/THEN logic does not account for the missing value you passing to the DAY() function calls.

The variable TODAY is never created in the data step. So just remove the %LET statement and add an actual assignment statement instead.

DATA _NULL_;
    today=today();
    if day(today) ge 1 and day(today) le 15 then do;
...

Just because you used the same name for the macro variable in the %LET statement as you used for the variable in the data step does not imply that the two have anything at all to do with each other.

If you wanted to use the macro variable to generate the code for the data step you would need to replace the TODAY with &TODAY.

    if day(&today) ge 1 and day(&today) le 15 then do;

So for the value you set to the macro variable TODAY it would mean that the SAS code you are trying to run is:

    if day(today()) ge 1 and day(today()) le 15 then do;

Note that is actually not a good way to handle this problem because it is calling the TODAY() function multiple times. That could cause strange results if the data step started right before midnight. So the IF condition might be run on the 31st but when you get to the ELSE condition the clock has ticked over to the first of the next month.

Upvotes: 1

Related Questions