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

 

data have;
input ID $1-3 Visit $5-7;
cards; 
101 v1
101 v2
101 v3
101 v4
101 v5
102 v1
102 v2
102 v3
102 v5
103 v1
103 v2
103 v3
103 v4
;
RUN;

Looking for participant ID if v5 is present, however, v4 is missing then we need to flag. 

 

 

Expected Output should be

 

idvisitflag
102v1Missing visit 4
102v2Missing visit 4
102v3Missing visit 4
102v5Missing visit 4
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID $1-3 Visit $5-7;
cards; 
101 v1
101 v2
101 v3
101 v4
101 v5
102 v1
102 v2
102 v3
102 v5
103 v1
103 v2
103 v3
103 v4
;
RUN;
proc sql;
create table want as
select *,ifc(sum(visit='v5') ne 0 and sum(visit='v4') eq 0,'Missing visit 4',' ') as flag
 from have
  group by id;
quit;

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
input ID $1-3 Visit $5-7;
cards; 
101 v1
101 v2
101 v3
101 v4
101 v5
102 v1
102 v2
102 v3
102 v5
103 v1
103 v2
103 v3
103 v4
;
RUN;
proc sql;
create table want as
select *,ifc(sum(visit='v5') ne 0 and sum(visit='v4') eq 0,'Missing visit 4',' ') as flag
 from have
  group by id;
quit;
Patrick
Opal | Level 21

Below selects all IDs with a visit=5 and populates the "flag" if there is no visit=v4

data want;
	if _n_=1 then
		do;
/* 			dcl hash h1(dataset:'have'); */
			dcl hash h1(dataset:'have(where=(visit in ("v4","v5")))');
			h1.defineKey('id','visit');
			h1.defineDone();
		end;
	set have;
	if h1.check(key:id, key:'v5') = 0 then 
		do;
			if h1.check(key:id, key:'v4') ne 0 then flag='Missing visit 4';
			output;
		end;
run;

proc print data=want;
run;
PC7
Fluorite | Level 6 PC7
Fluorite | Level 6

Thank you so much for the quick solution. Both the codes am getting as expected result. 

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
  • 4 replies
  • 864 views
  • 3 likes
  • 3 in conversation