I posted a previous question which was answered by @ballardw. see original post here:
https://communities.sas.com/t5/SAS-Programming/Force-zeros-into-multiple-fields/m-p/781563#M249068
But I need to make a small modification and getting errors with everything I try.
If location is Absent or Present (there are other values listed in location. I just didn't list them in the sample below), I want all zone fields to be zero if they aren't already populated. I have about 30 zone fields in my actual dataset so manually typing each field out isn't ideal.
| 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 |
How can I modify the array @ballardw suggested to capture the "Present" location as well?
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;
It would be helpful if you provided the code to generate the "have" dataset, as the table and code you supplied appear to contradict each other.
So I'm assuming Zone1-4 are numeric values as that's what the code refects. In which case this would work:
data have ;
infile cards missover ;
input patient $ visit $ Location $ Zone1 Zone2 Zone3 Zone4 ;
cards ;
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 2 1 . 0.3
;
run ;
data want;
set have;
array z (*) zone1 - zone4 ;
if location in ('Absent','Present') then do i=1 to dim(z);
if z[i] = . then z[i]=0 ;
end;
drop i;
run;
It would be helpful if you provided the code to generate the "have" dataset, as the table and code you supplied appear to contradict each other.
So I'm assuming Zone1-4 are numeric values as that's what the code refects. In which case this would work:
data have ;
infile cards missover ;
input patient $ visit $ Location $ Zone1 Zone2 Zone3 Zone4 ;
cards ;
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 2 1 . 0.3
;
run ;
data want;
set have;
array z (*) zone1 - zone4 ;
if location in ('Absent','Present') then do i=1 to dim(z);
if z[i] = . then z[i]=0 ;
end;
drop i;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.