BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChuksManuel
Pyrite | Level 9

Can anyone help me review this code? I'm trying to create a composite variable (abuse) but also to create a situation where any missing value across the 5 variables is coded as missing in the new composite variable.

if ace_swear =1 or ace_hurt=1 or ace_touch=1 or ace_tthem=1 or ace_sex=1 
then abuse=1;
if ace_swear =. and ace_hurt=. and ace_touch=. and ace_tthem=. and ace_sex=. 
then abuse=.;
else abuse=0;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You missed an else.

 

if ace_swear =1 or ace_hurt=1 or ace_touch=1 or ace_tthem=1 or ace_sex=1 
then abuse=1;
ELSE if ace_swear =. and ace_hurt=. and ace_touch=. and ace_tthem=. and ace_sex=. 
then abuse=.;
else abuse=0;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Your question is deficient. It doesn't say what the desired output is if there are no missings. (It does say what should happen if there are missings)

 

SAS is the best "person" to review the code. Run it on sample data and see if it gives the expected output.

--
Paige Miller
ChuksManuel
Pyrite | Level 9

Hello,

 

It's not giving me any error. It's however not giving me what i want. I want the new variable created "abuse" to have 0,1 and missing. With "abuse" called to missing if an observation has missing in all the 5 composite variables. I will appreciate if you can refer me to materials to help me figure this out. I know i'm just missing a small piece of code.

ChuksManuel_0-1684344580245.png

 

Reeza
Super User

You missed an else.

 

if ace_swear =1 or ace_hurt=1 or ace_touch=1 or ace_tthem=1 or ace_sex=1 
then abuse=1;
ELSE if ace_swear =. and ace_hurt=. and ace_touch=. and ace_tthem=. and ace_sex=. 
then abuse=.;
else abuse=0;
ChuksManuel
Pyrite | Level 9
Thanks!
Tom
Super User Tom
Super User

@ChuksManuel wrote:

Hello,

 

It's not giving me any error. It's however not giving me what i want. I want the new variable created "abuse" to have 0,1 and missing. With "abuse" called to missing if an observation has missing in all the 5 composite variables. I will appreciate if you can refer me to materials to help me figure this out. I know i'm just missing a small piece of code.

ChuksManuel_0-1684344580245.png

 


Sounds like you don't need IF/THEN at all.  Just use MAX() function.

abuse = max( of ace_swear ace_hurt ace_touch ace_tthem ace_sex);

 

Let's test the idea by making some data that covers all possible combinations of two variables with 0,1 or . as possible values.

data test;
do x=.,0,1; do y=.,0,1; 
  want=max(of x y);
  output;
end; end; 
run;

Result

Obs    x    y    want

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

 

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1002 views
  • 2 likes
  • 4 in conversation