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

Hi,

 

Here is how my data looks:

 

ID    Type

1      4045

1      0014

1      0100

1      0038

2      0014

2      4045 

2      0006

 

I want to identify all IDs where Type=0100 (this means the procedure did not run so the other types aren't accurate), while keeping the data the same when there is no Type 0100 for the ID.

 

So in this case my data would look like this:

 

ID    Type2

 

1      procedure didn't run

2      0014

2      4045 

2      0006

 

How would I do this? I'm thinking it may have to do with using first and last.

 

AC

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

@amanda_cr:

The simples solution I can think of is this:

data want;
  merge have have(where=(type='0100') in=failed);
  by ID;
  if failed then do; 
    if first.ID;
    type='Procedure did not run';
    end;
run;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Here's one way:

 

data want;

length type $ 20;

do until (last.id);

   set have;

   by id;

   if type = '0100' then situation = "procedure didn't run";

end;

do until (last.id);

   set have;

   by id;

   if situation = ' ' then output;

end;

if situation = "procedure didn't run" then do;

   type = situation;

   output;

end;

drop situation;

run;

 

This assumes that your data set is sorted by ID.  The top loop examines observations for one ID, to see if "0100" is found.  The second loop re-reads the same observations, possibly outputting them.

kiranv_
Rhodochrosite | Level 12

Below query should work.

data have;
input ID    Type $;
datalines;
1      4045
1      0014
1      0100
1      0038
2      0014
2      4045 
2      0006
;

proc sql;
create table want as 
select distinct id, type2 from 
(select id,
case when id  in (select id from have a
where trim(Type) ='0100') then 'procedure did not run' else Type end as Type2 
from have)a;
quit;
amanda_cr
Calcite | Level 5

Thanks everyone! All three solutions worked. I went with the SQL once because I prefer SQL.

s_lassen
Meteorite | Level 14

@amanda_cr:

The simples solution I can think of is this:

data want;
  merge have have(where=(type='0100') in=failed);
  by ID;
  if failed then do; 
    if first.ID;
    type='Procedure did not run';
    end;
run;
amanda_cr
Calcite | Level 5

Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1234 views
  • 0 likes
  • 4 in conversation