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

I have two datasets.
Dataset1: age_event variable is the age at which each ID reported an event. IDs that did not reported events were not included in this dataset.

data ds1;
input ID age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;

Dataset2: All IDs of the sample are reported. this dataset contains one row per ID. variable "last_agerecorded" is the at which each reported their last record.

data main;
input ID last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

We would like to create an "event" variable that equals to 1 if the event was reported and the corresponding age reported on "age_event". This is from dataset ds1. If an ID did not report an event, then event=0, and "age_event" should be equal to the age under "age_lastrecorded" from the dataset main. How can we get the required output below? Thank you.

IDage_eventevent
a1671
a2670
b1680
b2891
b331
c21210
c31240
c4580
d1890
d201
e2740
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

proc sort data=ds1;
  by ID;
run;
proc sort data=main;
  by ID;
run;

data want;
  merge main ds1(in=e);
  by ID;
  event=e;
  age_event=coalesce(age_event,last_agerecorded);
  drop last_agerecorded;
run;

proc print;run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

proc sort data=ds1;
  by ID;
run;
proc sort data=main;
  by ID;
run;

data want;
  merge main ds1(in=e);
  by ID;
  event=e;
  age_event=coalesce(age_event,last_agerecorded);
  drop last_agerecorded;
run;

proc print;run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ANKH1
Pyrite | Level 9

Thank you so much!

ballardw
Super User

This works with corrected data for your Main and Ds1 data sets. You did not indicate that ID should be read as a character value so would be all missing.

 

data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;
data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

/* requires both sets to be sorted by ID*/
data want;
   merge main (in=inmain)
         ds1  (in=inds1)
  ;
  by id;
  event=inds1;
  if inmain and not inds1 then age_event= last_agerecorded;

  drop last_agerecorded;
run;

The Merge will align two data sets one a BY variable. However the data step doesn't work well if both sets have repeated values for the by variable in both sets.

The IN= option creates a true/false (1/0) temporary variable that indicates whether the current observation has values from that set. So you can test conditionally for the case of observations in Main but not Ds1 as shown.

Sine the Event variable is basically that it came from DS1 that is using the Inds1 to set the Event variable.

ANKH1
Pyrite | Level 9
Thank you for explaining each step!
ChanceTGardener
SAS Employee
data ds1;
input ID $ age_event;
datalines;
a1 67
b2 89
b3 3
d2 0
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1 56
a2 67
b1 68
b2 72
b3 132
c2 121
c3 124
c4 58
d1 89
d2 95
e2 74
;
run;

proc sql;
 create table want as 
  select ID
  ,	     age_event
  ,	     1 as event
  from ds1
   outer union corresponding
  select ID
  ,	     last_agerecorded as age_event
  ,      0 as event
  from main
  where ID not in(select distinct ID from ds1)
 order by ID;
quit;

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
  • 5 replies
  • 475 views
  • 2 likes
  • 4 in conversation