BookmarkSubscribeRSS Feed
ak2011
Fluorite | Level 6

Hello,
Can someone please help me with the code to achieve this:
I am creating a variable called a1_level( with 3 levels 0,1 and 2) that considers r1,c1,f1
and d1. Each id should should have a level should meet a combination of 4 conditions:
Level 0: if r1=0 and c1=0 and f1= 0 and d1=0;
Level 2: if r1= 2 or r1=3 and if c1=2 or c1=3 and if f1=2 or f2= and if d1=2 or d1=3;
Level 1: if the subject doesn't meet the level 0 and level 2 conditions.
I created the levels 0 and 2 already but I don't know how to put in level 1; they are the missing
values in Table 2. ie. obs 11, id os13 should have 1. Same with id 15, 24 and 25.
My SAS code, log and results attached.
Please help me.
Thanks.
ak.
From Table 2,

/* Exposure data*/
data d1;
input id$ a1 a2 a3 a4 lung$ 14-21 a1s a2s a3s a4s r1 c1 f1 d1;
datalines;
os1 1 0 0 1 ca case 2 0 0 2 1 1 1 2
os2 1 1 0 0 ca case 1 1 0 0 3 2 3 2
os3 0 0 0 0 pop cont 0 0 0 0 0 0 0 0
os4 1 0 0 1 ca case 2 0 0 1 2 2 2 3
os5 0 1 0 0 ca case 0 1 0 0 0 0 0 0
os6 0 0 0 0 ca case 0 0 0 0 0 0 0 0
os7 1 0 1 1 pop cont 2 0 1 2 1 3 2 1
os8 0 1 0 0 ca case 0 2 0 0 0 0 0 0
os9 1 0 1 0 pop cont 2 0 2 0 3 3 2 2
os10 0 0 1 0 ca case 0 0 1 0 0 0 0 0
os11 0 1 0 0 pop cont 0 2 0 0 0 0 0 0
os12 0 1 0 0 pop cont 0 1 0 0 0 0 0 0
os13 1 1 1 1 pop cont 1 2 1 2 2 3 3 1
os14 0 0 0 0 pop cont 0 0 0 0 0 0 0 0
os15 1 0 0 1 ca case 2 0 0 1 2 1 1 3
os16 0 1 1 0 pop cont 0 2 2 0 0 0 0 0
os17 1 1 1 1 pop cont 2 1 2 1 3 2 2 2
os18 0 0 0 0 ca case 0 0 0 0 0 0 0 0
os19 0 1 0 0 pop cont 0 1 0 0 0 0 0 0
os20 0 0 0 0 ca case 0 0 0 0 0 0 0 0
os21 0 0 0 1 ca case 0 0 0 2 0 0 0 0
os22 1 1 1 0 ca case 1 1 2 0 2 3 3 2
os23 1 0 0 0 ca case 2 0 0 0 3 3 2 3
os24 1 1 0 1 pop cont 2 0 0 2 3 2 1 3
os25 1 1 1 0 ca case 1 1 2 0 2 3 1 2
os26 1 1 1 0 ca case 2 1 2 0 1 2 2 3
;

data d2(keep= id a1 a1s r1 c1 f1 d1 lung); set d1;
proc print data=d2; run;

proc freq data=d2;
tables a1*lung a1s*lung r1*lung c1*lung f1*lung d1*lung;
run;

/* Step 1: Delete r1=1 from dataset leaving only r=2 and r=3 + other relevant variables*/
data exp23;set d2;
if r1=1 then delete;
run;

proc print data=exp23;
title 'Table 1: Keep ever exposed(r=2 and r=3) + other relevant variables';
run;

proc freq data=exp23;
tables r1*lung c1*lung f1*lung d1*lung;
run;

/*Step 2: Create 3 levels: level 0, level 1 and level 2*/
data a1sns;
set exp23;
if r1=0 and c1=0 and f1=0 and d1=0 then a1_level=0;
else if r1>1 and c1 > 1 and f1>1 and d1>1 then a1_level=2;
else if c1 ne 1 and f1 ne 1 and d1 ne 1 then a1_level=1;
run;

proc print data=a1sns;
Title 'Table 2: al levels 0,1 and 2';
run;

proc freq data=a1sns;
tables r1*lung c1*lung f1*lung d1*lung;
run;

 
73
74 /* Exposure data*/
75 data d1;
76 input id$ a1 a2 a3 a4 lung$ 14-21 a1s a2s a3s a4s r1 c1 f1 d1;
77 datalines;
 
NOTE: The data set WORK.D1 has 26 observations and 14 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.02 seconds
 
 
104 ;
105
106 data d2(keep= id a1 a1s r1 c1 f1 d1 lung); set d1;
 
NOTE: There were 26 observations read from the data set WORK.D1.
NOTE: The data set WORK.D2 has 26 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
107 proc print data=d2; run;
 
NOTE: There were 26 observations read from the data set WORK.D2.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.69 seconds
cpu time 0.62 seconds
 
 
108
109 proc freq data=d2;
110 tables a1*lung a1s*lung r1*lung c1*lung f1*lung d1*lung;
111 run;
 
NOTE: There were 26 observations read from the data set WORK.D2.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.71 seconds
cpu time 0.69 seconds
 
 
112
113 /* Step 1: Delete r1=1 from dataset leaving only r=2 and r=3 + other relevant variables*/
114 data exp23;set d2;
115 if r1=1 then delete;
116 run;
 
NOTE: There were 26 observations read from the data set WORK.D2.
NOTE: The data set WORK.EXP23 has 23 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.02 seconds
 
 
117
118 proc print data=exp23;
119 title 'Table 1: Keep ever exposed(r=2 and r=3) + other relevant variables';
120 run;
 
NOTE: There were 23 observations read from the data set WORK.EXP23.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.23 seconds
cpu time 0.23 seconds
 
 
121
122 proc freq data=exp23;
123 tables r1*lung c1*lung f1*lung d1*lung;
124 run;
 
NOTE: There were 23 observations read from the data set WORK.EXP23.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.43 seconds
cpu time 0.44 seconds
 
 
125
126 /*Step 2: Create 3 levels: level 0, level 1 and level 2*/
127 data a1sns;
128 set exp23;
129 if r1=0 and c1=0 and f1=0 and d1=0 then a1_level=0;
130 else if r1>1 and c1 > 1 and f1>1 and d1>1 then a1_level=2;
131 else if c1 ne 1 and f1 ne 1 and d1 ne 1 then a1_level=1;
132 run;
 
NOTE: There were 23 observations read from the data set WORK.EXP23.
NOTE: The data set WORK.A1SNS has 23 observations and 9 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
133
134 proc print data=a1sns;
135 Title 'Table 2: al levels 0,1 and 2';
136 run;
 
NOTE: There were 23 observations read from the data set WORK.A1SNS.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.26 seconds
cpu time 0.26 seconds
 
 
137
138 proc freq data=a1sns;
139 tables r1*lung c1*lung f1*lung d1*lung;
140 run;
 
NOTE: There were 23 observations read from the data set WORK.A1SNS.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.49 seconds
cpu time 0.46 seconds
 
 
141
142 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
154

 




 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Please try this:

 

 data a1sns;
 set exp23;
 if r1=0 and c1=0 and f1=0 and d1=0 then a1_level=0;
 else if r1>1 and c1 > 1 and f1>1 and d1>1 then a1_level=2; 
 else a1_level=1;
 run;
--
Paige Miller
ak2011
Fluorite | Level 6
It works! Thanks very much.
ak.
Amir
PROC Star

Hi @ak2011 ,

 

Thanks for responding. In addition, marking the post from @PaigeMiller (not my post) as the solution will help everyone further.

 

Kind regards,

Amir.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 814 views
  • 0 likes
  • 3 in conversation