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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 860 views
  • 0 likes
  • 2 in conversation