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

Hi,

I have a dataset, I want to select the IDs who have had treatment (0) vs those who have not(>0) and create. The summary should have only one ID per subject.

 

The below is my data.

ID treatment

1 0

1 1

1 1

2 1

2 3

2 3

3 0

3 0

3 0

 

How it should look like

ID   Treatment(Dichotomous)

1     1

2     1

3     0

 

Thank you 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
tarheel13
Rhodochrosite | Level 12

next time, please post your data as datalines. 

data have;
input ID treatment;
datalines;
1 0
1 1
1 1
2 1
2 3
2 3
3 0
3 0
3 0
;
proc print;
run;

data have2;
	set have;
	trt=treatment > 0;
run;

proc sort data=have2;
	by id descending treatment;
run;

data want;
	set have2;
	by id descending treatment;
	if first.id;
run;

View solution in original post

3 REPLIES 3
tarheel13
Rhodochrosite | Level 12

next time, please post your data as datalines. 

data have;
input ID treatment;
datalines;
1 0
1 1
1 1
2 1
2 3
2 3
3 0
3 0
3 0
;
proc print;
run;

data have2;
	set have;
	trt=treatment > 0;
run;

proc sort data=have2;
	by id descending treatment;
run;

data want;
	set have2;
	by id descending treatment;
	if first.id;
run;
mkeintz
PROC Star

For each ID, read all the treatment>0 cases followed by all the without-treatment cases.  Then just keep the first obs for each id and test its TREATMENT variable against 0.

data have;
input ID treatment;
datalines;
1 0
1 1
1 1
2 1
2 3
2 3
3 0
3 0
3 0
run;
data want (drop=treatment);
  set have (where=(treatment>0)) have (where=(treatment=0));
  by id;
  if first.id;
  trt_dummy=(treatment>0);
run;

This works if the data are already sorted by ID.  But it doesn't matter what the order is within each ID.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input ID treatment;
cards;
1 0
1 1
1 1
2 1
2 3
2 3
3 0
3 0
3 0
;

proc sql;
create table want as
select id,sum(treatment>0) ne 0 as flag
 from have 
  group by id;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 388 views
  • 0 likes
  • 4 in conversation