Hello all,
I am trying to add two new variables to a dataset based on one condition. I believe I need to use an array and a do loop (?). Below is the code I have tried thus far but the resulting dataset does not output a 1 for both of my new variables when it should. I am new to SAS so any help is very much appreciated.
data want;
set have;
array new(*) patient;
gender=0;
job=0;
do i=1 to dim(new);
if patient="JOHNDOE" then gender=1 and job=1;
run;
The way SAS works for most data step operations you create the variable. Period. Conditionally you assign values to the variable.
So, which variables are the "new" variables?
The code you posted will not run but generate an a "unclosed DO" because there is no "End; " statement to close the "do i=1 to dim(new);"
Also, to assign values to multiple variables generally you need, if that is what this is supposed to do:
if patient="JOHNDOE" then gender=1 and job=1;
should be:
if patient ="JOHNDOE" then do; gender = 1; job = 1; end;
Did you read the log when submitting that code?
Something like "gender=1 and job=1" will attempt to return the value of the comparison: is gender=1 and job=1.
The way SAS works for most data step operations you create the variable. Period. Conditionally you assign values to the variable.
So, which variables are the "new" variables?
The code you posted will not run but generate an a "unclosed DO" because there is no "End; " statement to close the "do i=1 to dim(new);"
Also, to assign values to multiple variables generally you need, if that is what this is supposed to do:
if patient="JOHNDOE" then gender=1 and job=1;
should be:
if patient ="JOHNDOE" then do; gender = 1; job = 1; end;
Did you read the log when submitting that code?
Something like "gender=1 and job=1" will attempt to return the value of the comparison: is gender=1 and job=1.
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.