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

Hi,

I am trying to create a variable which reflects some perception on drug trafficking based on 6 other variables. The variables are all binary and for the new variable, if the values for the existing variables are 1, then the value for the new variable is 1. 

 

I used a code with an array as follows but I am not sure if I am the right path...

 

DATA modified;
SET Basics;
ARRAY VARIABLE drug_sell drug_store drug_traffic comm_norm_music
drug_comm_norm_hs drug_comm_music;
DO OVER VARIABLE;
IF (VARIABLE) = 1 THEN norm_traffic=1; ELSE norm_traffic=0;
END;
RUN;

 

Also, if I wanted to modify to code so that the new variable retains a value of 1 if 3 or more of the old variables have value of 1, how would I account for this in the code?

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

If one "true" is enough to get a true in your result, change your code:

norm_traffic = 0;
do over variable;
  if variable = 1 then norm_traffic=1;
end;

But you can get this easier:

norm_traffic = (sum(of variable{*}) > 0);

Untested, posted from my tablet.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

If one "true" is enough to get a true in your result, change your code:

norm_traffic = 0;
do over variable;
  if variable = 1 then norm_traffic=1;
end;

But you can get this easier:

norm_traffic = (sum(of variable{*}) > 0);

Untested, posted from my tablet.

benewaa
Fluorite | Level 6

Hi Kurt,

 

The correction to my code worked ok. the second, however, gives a value of 1 for all participants, which should not be the case.

On how to code so that when 3 or more of my existing variables have a 1, then my new variable assumes a value of 1, is there a way to modify the code for this?

ballardw
Super User

@benewaa wrote:

Hi Kurt,

 

The correction to my code worked ok. the second, however, gives a value of 1 for all participants, which should not be the case.

On how to code so that when 3 or more of my existing variables have a 1, then my new variable assumes a value of 1, is there a way to modify the code for this?


Data.

Example data.

And log of the actual code run.

norm_traffic3 = (sum(of variable{*}) ge 3);

would do the 3 or more.

SAS will return 1 for true and 0 for false for comparisons.

benewaa
Fluorite | Level 6

Hi Kurt,

 

This worked. Thanks a bunch

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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