BookmarkSubscribeRSS Feed
alexluther19
Calcite | Level 5

Hi everyone,

 

I am trying to create a new ordinal variable from a set of responses to other non-mutually exclusive binary variables. I believe I am running into trouble with some of my commands overwriting others, resulting in my frequencies being incorrect for the new variable. Can anyone help me correct the code below, or suggest a better methods for creating this variable?

 

I have tried coding many iterations but basically they come down to two main ways:

 

#1

data=dataset1

set dataset

anymental=.;

if anxiety=0 then anymental=0;

if mood=0 then anymental=0;

if eating=0 then anymental=0;

if anxiety=1 then anymental=1;

if mood=1 then anymental=2;

if eating=1 then anymental=3;

run;

 

#2

data=dataset1

set dataset

anymental=.;

if anxiety=0 then anymental=0;

if anxiety=1 then anymental=1;

if mood=0 then anymental=0;

if mood=1 then anymental=2;

if eating=0 then anymental=0;

if eating=1 then anymental=3;

run;

 

Neither is giving me the correct frequencies that I'd like to see for the anymental variable.

 

 

3 REPLIES 3
Tom
Super User Tom
Super User

You have two problems.

 

One is at the planning phase:

 

If you have 3 input binary variables (anxiety, mood , eating) you have 8 possible combinations.

You seem to be trying to map those into just 4 different values.

 

One is at the execution phase:

Your logic is independent.  So if EATING=1 then it does not matter what values the other two variables has because that last IF/THEN will set the value to 3.

 

Make yourself a truth table.  For every possible combination of the three variables what do you want for the output variable?

 

Here is what your current code does:

Obs    anxiety    mood    eating    anymental

 1        0         0        0          0
 2        0         0        1          3
 3        0         1        0          2
 4        0         1        1          3
 5        1         0        0          1
 6        1         0        1          3
 7        1         1        0          2
 8        1         1        1          3

So what did you want to do?  Did you just want to COUNT them?  Is so the add them up.  You could use the SUM() function.

  anymental=sum(anxiety,mood,eating);

Result

Obs    anxiety    mood    eating    anymental

 1        0         0        0          0
 2        0         0        1          1
 3        0         1        0          1
 4        0         1        1          2
 5        1         0        0          1
 6        1         0        1          2
 7        1         1        0          2
 8        1         1        1          3

  

 

alexluther19
Calcite | Level 5

Thanks for your reply, its super helpful to conceptualize the issue through a truth table!

 

I'm wondering now if what I'm looking to do is not possible, because I want the output variable to count some observations twice. For example, I want anymental for obs 4 to reflect both the mood (2) and the eating (3) values.

 

Am I approaching this wrong in terms of what I want from anymental? The reason for creating anymental as an ordinal variable is to then be able to perform regression analyses to compare associations between the various health conditions contained within anymental and various sociodemographic factors.

Tom
Super User Tom
Super User

You already have that with the existing variables.  Just include them all in the model.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 378 views
  • 1 like
  • 2 in conversation