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

Hi! 

 

How I can create a flag for repeating value. This is the case. If a subject reappears in the same study for the same drug then no flag. But if the same subject appears in another study for the same drug then flag = "Y" for both studies.

Here is sample data with flag values which I want to derive.

 

study   subject    drug   flag

01       1000          D1      Y

01       1001          D2 

01       1001          D2

01       1004          D1

02       1000          D1       Y

02       1001          D7

 

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

This code creates a flag that is 1 if the drug comes from two (or more) studies, and 0 otherwise.

 

proc sql;
	create table want as select *,count(distinct study)>1 as flag from have
	group by subject,drug order by study,subject,drug;
quit;

 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

This code creates a flag that is 1 if the drug comes from two (or more) studies, and 0 otherwise.

 

proc sql;
	create table want as select *,count(distinct study)>1 as flag from have
	group by subject,drug order by study,subject,drug;
quit;

 

--
Paige Miller
sonicview
Fluorite | Level 6
Thank you! It worked 🙂
Reeza
Super User

Here's one way - and it also adds a count to indicate how many studies the person was in with that drug. 

 

data have;
infile cards truncover;
input study   subject    drug  $ flag $;
cards;
01       1000          D1      Y
01       1001          D2 
01       1001          D2
01       1004          D1
02       1000          D1       Y
02       1001          D7
;;;;

proc sql;
create table want as
select *, count(distinct study) as num_studies, 
	case when calculated num_studies > 1 then 'Y' 
	else " " 
	end as calculated_flag
from have
group by subject, drug
order by study, subject;
quit;

@sonicview wrote:

Hi! 

 

How I can create a flag for repeating value. This is the case. If a subject reappears in the same study for the same drug then no flag. But if the same subject appears in another study for the same drug then flag = "Y" for both studies.

Here is sample data with flag values which I want to derive.

 

study   subject    drug   flag

01       1000          D1      Y

01       1001          D2 

01       1001          D2

01       1004          D1

02       1000          D1       Y

02       1001          D7

 

 

Thank you!


 

novinosrin
Tourmaline | Level 20

Sir @PaigeMiller  seems to be in supreme form lately. Kudos!

 

@sonicview  If you are really after Y and ' ', you could use IFC as an extension in Mr Miller's logic-

proc sql;
 create table want as 
 select *,ifc(count(distinct study)>1,'Y',' ') as flag 
 from have
 group by subject,drug 
 order by study,subject,drug;
quit;

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