Hi,
I have a dataset where I have years 0-8 and am only interested in years 0-2 for now. I want to compare between subjects who never used drugs and subjects who are currently using drugs (at year 2). I also need to compare between subjects who did not report drug use at year 2, but have reported drug use previously to the subjects who have never reported drug use.
drug use variable: hard_drugs
Time variable: years
Also, the same subjects were followed over 8 years, so there are repeated measures.
I used this code below to get the current drug users.
DATA CurrentDrugUsers;
SET PROJECT1.INPUT;
WHERE years = 2 AND hard_drugs = 1;
RUN;
I was able to get subjects who are currently not taking drugs, however, I cannot separate them into "previous" vs "never" groups.
DATA NeverDrugUsers;
SET PROJECT1.INPUT;
IF years = 2 AND hard_drugs = 0 ;
RUN;
Providing sample data and the desired result reduces ambiguity. Below two options for how I understand your question.
data have;
input subject $ year hard_drugs;
datalines;
a 0 0
a 1 0
a 2 0
b 0 1
b 1 0
b 2 0
c 0 0
c 1 1
c 2 1
;
/* option 1 */
proc sort data=have out=inter;
by subject year;
where year<=2;
run;
data want_1;
set inter;
by subject year;
retain past_hard_drugs_flg 0;
if hard_drugs=1 then past_hard_drugs_flg=1;
if last.subject then
do;
output;
past_hard_drugs_flg=0;
end;
run;
/* option 2 */
data want_2;
if _n_=1 then
do;
dcl hash h1(dataset:'have(where=(year<2 and hard_drugs=1))');
h1.defineKey('subject');
h1.defineDone();
end;
set have (where=(year=2));
past_hard_drugs_flg= not h1.check();
run;
It would seem to me that you're going to need to make a flag for ever drug use. You can get an ever flag a couple of different ways. Most common is probably proc sql or retain statement.
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!
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.
Ready to level-up your skills? Choose your own adventure.