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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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