BookmarkSubscribeRSS Feed
raheleh22
Obsidian | Level 7

Hi,
i have three categorical variables in my data set:
1. alcohol (current=1, other=0)
2. smk (current=1, other=0)
3.drug (current=1, other=0)
now i want to creat a variable that only includes level1 of these variables:
here is what I wrote:
if alcohol=1 & smk=1 & drug=1 then (new variable name)= 1; else (new variable)=0;
however this is not showing in my proc freq.
any advice is appreciated.
Thanks

5 REPLIES 5
ballardw
Super User

Are your variables numeric or character? If you write code to compare character values to numeric things quite often go not as expected.

 

If you have a syntax problem a previous version of the data set would not be replaced.

Are you sure that you ran proc freq against a new data set? (Hope you created a new data set or you may be spending a lot of time recreating the original data.)

raheleh22
Obsidian | Level 7

so here is my sas code:

Data KNP;/*grade= studetn's grade*/

set Pratap_KN_Analysis;

if ethncty= 5 then ethnicitycat=1;

else ethnicitycat=0;

if Relign= 1 then religioncat=1;

else religioncat=0;

/*we need to merge marijuana with drug use**************************/

if  Rcnt_Drug=1 or Rcnt_Mrjuna=1 then drug_use=1;

else drug_use=0;

/********my question is this part**************************************/

if Crnt_Alcohol=1 & Crnt_use_Smoke=0 & drug_use=0 then healthrisk1=1;

else healthrisk1=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=1 & drug_use=0 then healthrisk2=1;

else healthrisk2=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=0 & drug_use=1 then healthrisk3=1;

else healthrisk3=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=1 & drug_use=0 then healthrisk4=1;

else healthrisk4=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=0 & drug_use=1 then healthrisk5=1;

else healthrisk5=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=1 & drug_use=1 then healthrisk6=1;

else healthrisk6=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=1 & drug_use=1 then healthrisk7=1;

else healthrisk7=0;

if healthrisk1 or healthrisk2 or healthrisk3=1 then healthrisk=1;

if healthrisk4=1 or healthrsik5=1 or healthrisk6=1 then healthrisk=2;

if healthrisk7=1 then healthrisk=3;

else healthrisk =0;

if Q32 = 7 then physical = 0;

else physical = 1;

if Q72 <=3 then friends = 0;

else friends =1;

if Q74 <=3 then family = 0;

else family =1;

run;

Reeza
Super User
if healthrisk1 or healthrisk2 or healthrisk3=1 then healthrisk=1;

if healthrisk4=1 or healthrsik5=1 or healthrisk6=1 then healthrisk=2;

if healthrisk7=1 then healthrisk=3;

else healthrisk =0;

This is one problem. That last ELSE conditions sets everything not 3 back to 0. 

 

It should be:

 

if healthrisk1 or healthrisk2 or healthrisk3=1 then healthrisk=1;

else if healthrisk4=1 or healthrsik5=1 or healthrisk6=1 then healthrisk=2;

else if healthrisk7=1 then healthrisk=3;

else healthrisk =0;

@raheleh22 wrote:

so here is my sas code:

Data KNP;/*grade= studetn's grade*/

set Pratap_KN_Analysis;

if ethncty= 5 then ethnicitycat=1;

else ethnicitycat=0;

if Relign= 1 then religioncat=1;

else religioncat=0;

/*we need to merge marijuana with drug use**************************/

if  Rcnt_Drug=1 or Rcnt_Mrjuna=1 then drug_use=1;

else drug_use=0;

/********my question is this part**************************************/

if Crnt_Alcohol=1 & Crnt_use_Smoke=0 & drug_use=0 then healthrisk1=1;

else healthrisk1=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=1 & drug_use=0 then healthrisk2=1;

else healthrisk2=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=0 & drug_use=1 then healthrisk3=1;

else healthrisk3=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=1 & drug_use=0 then healthrisk4=1;

else healthrisk4=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=0 & drug_use=1 then healthrisk5=1;

else healthrisk5=0;

if Crnt_Alcohol=0 & Crnt_use_Smoke=1 & drug_use=1 then healthrisk6=1;

else healthrisk6=0;

if Crnt_Alcohol=1 & Crnt_use_Smoke=1 & drug_use=1 then healthrisk7=1;

else healthrisk7=0;

if healthrisk1 or healthrisk2 or healthrisk3=1 then healthrisk=1;

if healthrisk4=1 or healthrsik5=1 or healthrisk6=1 then healthrisk=2;

if healthrisk7=1 then healthrisk=3;

else healthrisk =0;

if Q32 = 7 then physical = 0;

else physical = 1;

if Q72 <=3 then friends = 0;

else friends =1;

if Q74 <=3 then family = 0;

else family =1;

run;


 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1355 views
  • 0 likes
  • 4 in conversation