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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

1 REPLY 1
ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 1042 views
  • 0 likes
  • 2 in conversation