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 |
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.
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.
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;
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.
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.