budding pro
budding pro

Reputation: 195

Rewrite SAS code with condition based on dates

I have the following SAS code that I am trying to rewrite with condition. I am a SAS beginner and for now I can read and trying to learn code

DATA _NULL_;
    date1=put(INTNX('day',&today,-1),DATE11.);
    date2=put(INTNX('day',&today,-15),DATE11.);
    call symput('rpt_dt',date1);
    call symput('rpt_dt2',date2);
RUN;

instead of yesterday and two weeks ago, I would like the following condition:

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

I tried the following:

DATA _NULL_;
    if day(today) between 1 and 15 then
        date1 = put(intnx('month', &today, -1, 'e'),DATE11.);
    else
        date1 = put(intnx('month', &today, 0, 'b'),DATE11.);
    if day(date1) = 1 then
        date2 = put(intnx('day', &date1, 15, 'e'),DATE11.);
    else
        date2 = put(intnx('month', &date1, 0, 'e'),DATE11.);
    call symput('rpt_dt',date1);
    call symput('rpt_dt2',date2);
RUN;


DATA _NULL_;
    if day(today) ge 1 and day(today) le 15 then
        date1=put(intnx('month',today,-1,'E'), date11.);
        date2=put(intnx('month',today,-1,'L'), date11.);
    else
        date1=put(intnx('month',today,0,'B'), date11.);
        date2=put(intnx('month',today,0,'L'), date11.);
    call symput('rpt_dt',date1);
    call symput('rpt_dt2',date2);
RUN;



but each one was not successful and I get error in the between, the do, or missing then.

Upvotes: 0

Views: 35

Answers (1)

budding pro
budding pro

Reputation: 195

Since SAS has a different syntax, the if-then should be explicit.

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

Upvotes: 0

Related Questions