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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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