BookmarkSubscribeRSS Feed
karns
Calcite | Level 5

I have a data set containing many participants. Each participant has multiple events. For each participant and each event, I want a list of the variable value. This might be best described by demonstration:

For example,

PID     EVENT     VAR

1392     1               .

1392     1               13  

1392     2               12    

1392     2               12  

2456     1               3   

2456     1               . 

2456     2              .

2456     2               .

I want to end up with a list like this:

PID     EVENT     VAR

1392     1               13  

1392     2               12    

2456     1               3   

2456     2              .

Is there a particular method for doing this? Participants have more than one EVENT. And within each EVENT, VAR is the same (but some observations are .) In some cases, VAR is . for the entire event. In other cases, VAR is . for only a few cases within an EVENT.

6 REPLIES 6
Reeza
Super User

Assuming you can't have records with all missing, then remove missing at the start and then keep only the first occurrence.

proc sort data=have;

by pid event;

run;

data want;

set have;

where var ne .;

by pid event;

if first.event;

run;

stat_sas
Ammonite | Level 13

data want;
set have;
if event=1 and var=. then delete;
run;

proc sql;
select distinct pid,event,var
from want;
quit;

eirhpaul
Calcite | Level 5

Let's call your first data example ds1 to get ds2 (second data example) try the following:

data ds2 (drop = issetflg);

     retain issetflg;

     set ds1;

     by pid event;

     if first.event then issetflg = 'N';

     if var ne . then do;

          issetflg = 'Y';

          output;

     end;

     if last.event then

          if issetflg = 'N' then

               output;

run;

FloydNevseta
Pyrite | Level 9

Assuming the data is already sorted by pid and event, try this:

data want;

set have;

by pid event;

retain found;

if first.event then found = 0;

if found = 0 and ( not missing( var ) or last.event ) then do;

     output;

     found = 1;

end;

run;

slchen
Lapis Lazuli | Level 10


proc sql;
select distinct pid,event,var from have group by pid,event having var=max(var);
quit;

FloydNevseta
Pyrite | Level 9

Here's another approach:

data want;

set have (rename=(var=varx));

by pid event;

retain var;

if first.event then var = .;

var = coalesce( var, varx );

if last.event then output;

drop varx;

run;

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
  • 6 replies
  • 13514 views
  • 0 likes
  • 6 in conversation