BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Travis
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

3 REPLIES 3
Haikuo
Onyx | Level 15

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.

Peter_C
Rhodochrosite | Level 12

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

Travis
Calcite | Level 5

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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