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

Hello,

 

I currently have a data set of events with the month and year recorded for each event. For each person i want to find which event happens first and then find the year and month of this event.

 

Example data

 

ID     E1   M1      Y1          E2   M2      Y2 

001    1       7    1999        0      .         .

002    1       1     2004       1       9       2001

003    1       3     2006       1      11      2006

 

etc...

 

Want

 

FirstE      FirstMonth   FirstYear

E1                  7                 1999

E2                  9                  2001

E1                  3                  2006

 

The code I have currently been trying i cant get to work so any suggestions would be really helpful!

 

data want;
set have;
Year = min(Y1, Y2); array Years (*) Y1 Y2; array Months (*) M1 M2; if Year = Y1 = Y2 then
FirstE = vname(Months[whichn(smallest(1,of Months[*]), of Months[*])]); else if Y1 ^= Y2 then
FirstE = vname(Years[whichn(smallest(1,of Years[*]), of Years[*])]);

if FirstE = "E1" then do;
FirstMonth = M1; FirstYear = Y1; end; else
if FirstE = "E2" then do;
FirstMonth = M2; FirstYear = Y2; end;
RUN;

 

Thanks

Cydney

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use function mdy() and take proper care of missing values:

 

data want;
set have;
d1 = coalesce(mdy(M1, 1, Y1), constant("BIG"));
d2 = coalesce(mdy(M2, 1, Y2), constant("BIG"));
if d1 <= d2 then do;
    FirstE = "E1";
    firstYear = Y1;
    FirstMonth = M1;
    end;
 else if d1 > d2 then do;
    FirstE = "E2";
    firstYear = Y2;
    FirstMonth = M2;
    end;
drop d1 d2;
run;

(untested)

PG

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

Use function mdy() and take proper care of missing values:

 

data want;
set have;
d1 = coalesce(mdy(M1, 1, Y1), constant("BIG"));
d2 = coalesce(mdy(M2, 1, Y2), constant("BIG"));
if d1 <= d2 then do;
    FirstE = "E1";
    firstYear = Y1;
    FirstMonth = M1;
    end;
 else if d1 > d2 then do;
    FirstE = "E2";
    firstYear = Y2;
    FirstMonth = M2;
    end;
drop d1 d2;
run;

(untested)

PG
CydneyLB
Obsidian | Level 7

 

Thank you for commenting!

I was trying to avoid creating a date variable (for later calculations) but that will work.

 

Thanks 🙂

Cydney

novinosrin
Tourmaline | Level 20

FWIW

 


data have;
Input ID $     E1   M1      Y1          E2   M2      Y2 ;
cards;
001    1       7    1999        0      .         .
002    1       1     2004       1       9       2001
003    1       3     2006       1      11      2006
;

data temp;
 set have;
 array t(2,3) E1--y2;
 array j FirstE      FirstMonth   FirstYear;
 do _i=1 to 2;
  call missing(temp);
  do _k=1 to 3;
   j(_k)=t(_i,_k);
  end;
  if n(of j(*))=dim(j) then temp=mdy(firstmonth,1,firstyear);
  output;
 end;
keep first: temp id;
run;
proc sql;
create table want(drop=temp) as
select *
from temp
group by id
having temp=min(temp);
quit;
Jagadishkatam
Amethyst | Level 16

Alternatively try

 

data want;
set have;
array event(*) E1 E2;
array months(*) M1 M2;
array years(*) Y1 Y2;
do i = 1 to dim(event);
if event(i) ne . then e=vname(event(i));
if month(i) ne . then m=months(i);
if year(i) ne . then y=years(i);
output;
end;
run;

proc sort data=want;
by id y m;
where y ne .;
run;

data want2;
set want;
by id y m ;
if first.id;
keep id e m y;
run;
Thanks,
Jag
CydneyLB
Obsidian | Level 7

This solution is nice and simple thanks Jag Smiley Very Happy

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1230 views
  • 3 likes
  • 4 in conversation