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

I'm trying to figure out how to use yes/no statement in my if/then statement. How do I apply this update to the dataset variable using a character yes/no format?

 

Data final;

 

Set data_thesis;

 

Where 20<=RIDAGEYR<=35 & RIDRETH3=4 & RIAGENDR=2;

 

array _dpq dpq:;

do over _dpq;

if (_dpq >= 7) then call missing(_dpq);

end;

Depression_Score = sum(of dpq010-dpq090);

if 0 <= Depression_Score < 10 then Depression_Indicator = 'No';

else if Depression_Score >= 10 then Depression_Indicator = 'Yes';


if sddsrvyr in (8,9) then MEC4YR = 1/2 * WTMEC2YR;

if riagendr = 1 then Gender = 'Male ';

else if riagendr =2 then Gender = 'Female';

if LBXTC > 180 then Cholesterol = 'Prevalent ';
else if 0<=LBXTC<=180 then Cholesterol = 'Not Prevalent';

if LBDHDD > 60 then HDL = 'Prevalent';


else if 0<=LBDHDD<=60 then HDL = 'Not Prevalent';;


if LBXTR > 150 then Triglycerides = 'Prevalent ';

else if 0<=LBXTR<=150 THEN Triglycerides = 'Not Prevalent';

if LBDLDL > 100 then LDL = 'Prevalent';

else if 0<=LBDLDL<=100 then LDL = 'Not Prevalent';

keep SEQN Depression_Score Depression_Indicator Cholesterol YesNo MEC4YR HDL LDL Triglycerides Gender RIDAGEYR RIDRETH3 RIAGENDR SDMVPSU WTMEC2YR SDMVSTRA LBXTC LBDLDL LBXTR LBDHDD;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

I generally suggest that instead of character values like yes/no true/false, has condtion x/ does not to use numeric values of 1/0.

So instead of

if 0 <= Depression_Score < 10 then Depression_Indicator = 'No';
else if Depression_Score >= 10 then Depression_Indicator = 'Yes';

Something like

if not missing(Depression_Score) then Depression_Indicator =(Depression_Score >= 10);

SAS will create 1/0 values for true/false comparisons.

One reason to use 1/0 coded values is you can request the SUM of the variable to get the number of 1(or Yes) values and the Mean is the percent yes.

A custom format can display the text Yes/No when needed:

Proc format;
value yn
1='Yes'
0='No'
other=' ';
run;

A similar format could display the words "Prevalent" or "not prevalent" by setting the 1/0 condition.

OR since your values appear to be numeric to begin based on single variables you could likely skip creating the new variables entirely.

proc format;
value depression
0 -<10 = "No"
10-high = "Yes"
;
value gender
1='Male'
2='Female'
;
value lbxtc
0 - 180 ='Not Prevalent'
180<high = 'Prevalent'
;
/* follow the pattern for LBDHDD etc*/
run;

proc print data=data_thesis;
   var Depression_Score riagendr  LBXTC ;
   format Depression_Score depression.   riagendr gender.
      LBXTC LBXTC.;
   ;
run;

The groups created by formats are generally usable by analysis, graphing and report procedures.


The difference might come in if you need/want to do something with how many conditions have a 'Yes' or 'prevalent' condition. Then if you use the 1/0 coding then summing them gives you a per person count.

Without duplicating a lot of if/then/else code in counting.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

In SAS the word FORMAT has a specific meaning. A format is instructions for how to display a value as text.  It doesn't sound like you are actually talking about any formats.  I don't see any formats being defined or used in your code.

 

It sounds like you are just asking something about how to use a character variable that has value like 'Yes' or 'No' ?

Which variable is that? 

Is it one that existed in the input dataset?  Or are you trying to create it in this step?  If you are creating it make sure to define it as being at least three bytes long so that there is room to store both No and Yes.

 

What is it that you want to do with this variable that is causing your trouble?

ED18
Calcite | Level 5

I am creating the step but I want a yes/no value for my output data for depression 

Tom
Super User Tom
Super User

@ED18 wrote:

I am creating the step but I want a yes/no value for my output data for depression 


My mind reading powers are off today.  What exactly is your question?

Are you asking how to create a custom format that will display values of Depression_Score as No or Yes?

proc format ;
 values depyesno
  0 - <10 = 'No'
10 - high = 'Yes'
 ;
run;

proc freq data=final;
  tables depression_score;
  format depression_score depyesno.;
run;
ballardw
Super User

I generally suggest that instead of character values like yes/no true/false, has condtion x/ does not to use numeric values of 1/0.

So instead of

if 0 <= Depression_Score < 10 then Depression_Indicator = 'No';
else if Depression_Score >= 10 then Depression_Indicator = 'Yes';

Something like

if not missing(Depression_Score) then Depression_Indicator =(Depression_Score >= 10);

SAS will create 1/0 values for true/false comparisons.

One reason to use 1/0 coded values is you can request the SUM of the variable to get the number of 1(or Yes) values and the Mean is the percent yes.

A custom format can display the text Yes/No when needed:

Proc format;
value yn
1='Yes'
0='No'
other=' ';
run;

A similar format could display the words "Prevalent" or "not prevalent" by setting the 1/0 condition.

OR since your values appear to be numeric to begin based on single variables you could likely skip creating the new variables entirely.

proc format;
value depression
0 -<10 = "No"
10-high = "Yes"
;
value gender
1='Male'
2='Female'
;
value lbxtc
0 - 180 ='Not Prevalent'
180<high = 'Prevalent'
;
/* follow the pattern for LBDHDD etc*/
run;

proc print data=data_thesis;
   var Depression_Score riagendr  LBXTC ;
   format Depression_Score depression.   riagendr gender.
      LBXTC LBXTC.;
   ;
run;

The groups created by formats are generally usable by analysis, graphing and report procedures.


The difference might come in if you need/want to do something with how many conditions have a 'Yes' or 'prevalent' condition. Then if you use the 1/0 coding then summing them gives you a per person count.

Without duplicating a lot of if/then/else code in counting.

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!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 6473 views
  • 2 likes
  • 3 in conversation