BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

I need your support for a quite complicated task. 

 

I have the following 

 

 

data DB1;
input ID :$20. (Start End)(:date9.);
format Start End date9.;
cards;
0001 01JAN2015 06FEB2017 
0001 08FEB2017 25APR2018 
0001 26APR2018 31DEC2021 
0002 01JAN2017 12JUL2017 
0002 13JUL2017 31DEC2019 
0002 01JAN2020 31OCT2021
;

 

what I need to do is to add a flag for each year if the ID appears in the *year. Then for each year I need to count one time the flag for each ID if it appears more than one time (but I can do this by myself).

The desired output would be:

 

 

data DB1;
input ID :$20. (Start End)(:date9.) F2015 :$20. F2016 :$20. F2017 :$20. F2018 :$20. F2019 :$20. F2020 :$20. F2021 :$20.;
format Start End date9.;
cards;
0001 01JAN2015 06FEB2017 1 1 1 0 0 0 0
0001 08FEB2017 25APR2018 0 0 1 1 0 0 0
0001 26APR2018 31DEC2021 0 0 0 1 1 1 1
0002 01JAN2017 12JUL2017 0 0 1 0 0 0 0
0002 13JUL2017 31DEC2019 0 0 1 1 1 0 0
0002 01JAN2020 31OCT2021 0 0 0 0 0 1 1
;

data want;
input ID :$20.A2015 :$20. A2016 :$20. A2017 :$20. A2018 :$20. A2019 :$20. A2020 :$20. A2021 :$20.;
cards;
0001 1 1 1 1 1 1 1
0002 0 0 1 1 1 1 1
;



 

My point is that periods are aggregated across years so that year(Start) is ineffective. Can anyone help me please?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way:

data temp;
   set db1;
   array a(2015:2021) a2015 - a2021;
   do i=2015 to 2021;
      a[i]= (year(start) le i le year(end));
   end;
   keep id a: ;
run;

proc summary data=temp nway;
   class id;
   var a: ;
   output out=want (drop=_:) max=;
run;

Of course, after you put data, as in a year value, into a variable name it becomes much harder to work with in general.

What will you do with the Want data set.

View solution in original post

3 REPLIES 3
Reeza
Super User

Retain on the array variables and reset it at the start of each ID.

So as you set it to one it stays as one until it's reset.

ballardw
Super User

One way:

data temp;
   set db1;
   array a(2015:2021) a2015 - a2021;
   do i=2015 to 2021;
      a[i]= (year(start) le i le year(end));
   end;
   keep id a: ;
run;

proc summary data=temp nway;
   class id;
   var a: ;
   output out=want (drop=_:) max=;
run;

Of course, after you put data, as in a year value, into a variable name it becomes much harder to work with in general.

What will you do with the Want data set.

NewUsrStat
Lapis Lazuli | Level 10
Thank you very much! I just need to do summary stats with "want".

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 884 views
  • 1 like
  • 3 in conversation