BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasecn
Quartz | Level 8

I am trying to get work active flags for each month during past 2 years depending on work start and end dates. My sample is like:

work_idwork_startwork_end
115JUN202215JUN2023
215MAY202110MAY2022

So, my result will add 24 work active flags from Jan. 2021 to Dec. 2022. Work is active for each month if start before last day of month and end after first of month, result looks like:

work_idwork_startwork_end....active_3_2022active_4_2022active_5_2022active_6_2022...
115MAY202215JUN2023...0011...
215MAY202110MAY2022...1110...
1 ACCEPTED SOLUTION

Accepted Solutions
sasecn
Quartz | Level 8

Thanks for your reply. I got an error when I tried the code. I first create the test data just like my first table:

work_idwork_startwork_end
115-Jun-2215-Jun-23
215-May-2110-May-22

I called it "have", when I ran the code, I got this:

sasecn_0-1666578837501.png

Any idea, what it would be?

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Let me suggest that you change the names of the flags slightly.  For example, instead of active_3_2022 use active_2022_03.  That will simplify some of the programming statements and make your variabe names easier to sort if that is needed.

 

Here's an untested solution:

data want;
   set have;
   array months {24} active_2021_01 - active_2021_12 active_2022_01 - active_2202_12;
   beginning = month(work_start) + 12 * (year(work_start) - 2021);
   ending = month(work_end) + 12 * (year(work_end) - 2021);
   do j = 1 to 24;
      months{j} = (beginning <= j <= ending);
   end;
   drop j beginning ending;
run;

It looks right.  But since yoiu're the one who has the data, you will need to be the one who tests it.  If there are any problems with it, please post the log.

sasecn
Quartz | Level 8

Thanks for your reply. I got an error when I tried the code. I first create the test data just like my first table:

work_idwork_startwork_end
115-Jun-2215-Jun-23
215-May-2110-May-22

I called it "have", when I ran the code, I got this:

sasecn_0-1666578837501.png

Any idea, what it would be?

Astounding
PROC Star

My fault, just a typo:  active_2202_12 should be active_2022_12

 

sasecn
Quartz | Level 8

Sorry, my bad, I should capture that. The code works. Thanks! Some part of the code, I still don't understand, but  I think I can figure it out by some additional readings. Thanks again!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 346 views
  • 1 like
  • 2 in conversation