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

Hi,

Using the sample dataset below, what is the best way to make zone1,zone2,zone3 and zone4 to all be zero if the location field is absent? if location field is present, then zone fields should remain as is. I'm trying to do this without writing a long if-then statement.

 

Patient Visit Location Zone1 Zone2 Zone3 Zone4
1 Week1 Absent        
1 Week2 Absent        
1 Week3 Present 1   0.5 1.5
2 Week1 Absent        
2 Week2 Absent        
2 Week3 Present 2 1   0.3
3 Week1 Present   1.5 0.9  
3 Week2 Absent        
3 Week3 Present 3 0.8 0 0
4 Week1 Present   0.9   0.6
4 Week2 Present 3 1.2 2  
4 Week3 Present 3   2  
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

When you have to do the same thing to multiple variables on a single observation then the typical tool would be an ARRAY.

Perhaps something like:

data want;
   set have;
   array z (*) zone1 - zone4 ;
   if location = 'Absent' then do i=1 to dim(z);
       z[i] = 0;
   end;
drop i; run;

Array statement creates a short cut way to allow accessing related variables. The Z is the name of the array and cannot be an existing variable, the (*) says that the size will be based on the list of variables provided and the zone1-zone4 is the list and expects there to be other variables named zone2 and zone3, sequentially numbered.

The Dim function returns how many elements are in the array so the loop executes once for each variable (in this case), setting each value to 0.

View solution in original post

6 REPLIES 6
ballardw
Super User

When you have to do the same thing to multiple variables on a single observation then the typical tool would be an ARRAY.

Perhaps something like:

data want;
   set have;
   array z (*) zone1 - zone4 ;
   if location = 'Absent' then do i=1 to dim(z);
       z[i] = 0;
   end;
drop i; run;

Array statement creates a short cut way to allow accessing related variables. The Z is the name of the array and cannot be an existing variable, the (*) says that the size will be based on the list of variables provided and the zone1-zone4 is the list and expects there to be other variables named zone2 and zone3, sequentially numbered.

The Dim function returns how many elements are in the array so the loop executes once for each variable (in this case), setting each value to 0.

Etoo12121
Obsidian | Level 7
Thank you. Arrays are perfect for my situation
ChrisNZ
Tourmaline | Level 20

For so few variables just code it and keep the code simple.

data WANT;
  set HAVE;
  if LOCATION = 'Absent' then do;
    ZONE1=0; ZONE2=0; ZONE3=0; ZONE4=0; 
  end;
run;

 

Etoo12121
Obsidian | Level 7
this works as well but unfortunately, I have more variables than the 4 zones I listed in the sample. I'll keep this in my back pocket. Thank you
andreas_lds
Jade | Level 19

Do you need the zeros in the dataset or in a report? If the later is the case, you could just use option missing="0", before creating the report.

Etoo12121
Obsidian | Level 7
I needed them in the dataset

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