DATA Step, Macro, Functions and more

Change Macro Variable based on condition of data step variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Change Macro Variable based on condition of data step variable

Hi,

I am trying to code in some logic to change the value of my macro variable if the value of my data step variable meets a certain condition. Specifically, I want to output an observation for each day within a sample, and identify those days that coincide with a monthly payroll cycle.

%let start      = '01jan2013'd; * start of sample;  

%let end        = '31dec2017'd; * end of sample;

%let day        = 30;           * payday (day of month);

%let geo        = US;           * sample geography;symg

    

%macro paycycle;

data paycycle;

format date date9.;

geo  = "&geo";

    

do date = &start to &end;

           if day(date) = &day then payday = 1;

           else payday = 0;

           output;

     end;

run;

%mend;

%paycycle

However, because some months are shorter than others, I need to change the value of my payday macro variable depending on the month. For example, the payday for February should be the 28th not the 30th. Is there a way to change the value of &day within the loop if depending on the value of date in my data step?

I realize this doesn’t work, but to illustrate my point, something like this:

do date = &start to &end;

     if month(date) = 2 then do;

%let day = 28;

                end;

           end;

Thanks so much!


Accepted Solutions
Solution
‎11-05-2014 05:00 PM
Respected Advisor
Posts: 3,124

Re: Change Macro Variable based on condition of data step variable

I suggest to keep it simple, only to pick up the month end when less than 30:

%macro paycycle;

data paycycle;

format date date9.;

geo = "&geo";

do date = &start to &end;

           if day(date) = min(day(intnx('month',date,0,'e')),&day) then payday = 1;

           else payday = 0;

           output;

end;

run;

%mend;; QUIT;

Haikuo

Update: In term of writing macros, I also suggest to use macro parameters (localize those macro variables), instead of bunch of %let in the open code.

View solution in original post


All Replies
Solution
‎11-05-2014 05:00 PM
Respected Advisor
Posts: 3,124

Re: Change Macro Variable based on condition of data step variable

I suggest to keep it simple, only to pick up the month end when less than 30:

%macro paycycle;

data paycycle;

format date date9.;

geo = "&geo";

do date = &start to &end;

           if day(date) = min(day(intnx('month',date,0,'e')),&day) then payday = 1;

           else payday = 0;

           output;

end;

run;

%mend;; QUIT;

Haikuo

Update: In term of writing macros, I also suggest to use macro parameters (localize those macro variables), instead of bunch of %let in the open code.

Valued Guide
Posts: 2,175

Re: Change Macro Variable based on condition of data step variable

Haikuo has provided the function call that identifies the "month end date"

     intnx('month',date,0,'e')

So you do not need the &day parameter nor the number of days in the month.... just derive with the test

    is DATE = last-day-of-month.

with the code

do date = &start to &end;

   payday= ( date=  intnx('month',date,0,'e') ) ;

   output ;

end;

Should the payday allways be the last day of a month?

Should it not be a "working day", or "WEEKDAY"?

 

There is a date interval that could look for the last WEEKDAY of the month.... check out the INTNX() function parameters

Occasional Contributor
Posts: 6

Re: Change Macro Variable based on condition of data step variable

Thank you for your responses!

The payday will NOT always be the end of the month. Sometimes it will be the 1st, other times the 15th, or the 31st. So Haikuo's answer should work!

Thank you very much.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 311 views
  • 3 likes
  • 3 in conversation