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

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;

  

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

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;

View solution in original post

2 REPLIES 2
AMSAS
SAS Super FREQ

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;
Etoo12121
Obsidian | Level 7
Perfect! Thank you

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
  • 2 replies
  • 758 views
  • 1 like
  • 2 in conversation