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
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.
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.
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?
@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.
Hi Kurt,
This worked. Thanks a bunch
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.