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

I have the following sample data for 5 weeks by doctor_ID

 

data have;

input id rx1-rx5;

cards;

12 1 0 4 0 3

13 0 2 0 0 0

14 0 0 3 1 0

15 2 1 0 0 2

 

I want to get the new writers each week. rx1-rx5 is the Rxs the doctors wrote for 1 thru 5 weeks. Rx1 being the latest week. Rx5 is oldest week.

 

Output should be as below:

 

ID nw1 nw2 nw3 nw4 nw5

12    0   0      0      0     1

13    0   1      0      0     0

14    0   0      0      1     0

15    0   0      0      0     1

 

I would appreciate any help in this

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Loop through the array from reverse to find the first non-zero?

untested

 

data want;
set have;

array rx(5);
array nw(5);

dp i=1 to 5;
nw(i)=0;
end;

do i=5 to 1 by -1
if rx(i) ne 0 then do;
nw(i) = 1;
leave;
end;
end;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Whats the logic/rules to get from rx to nw?

Reeza
Super User

Loop through the array from reverse to find the first non-zero?

untested

 

data want;
set have;

array rx(5);
array nw(5);

dp i=1 to 5;
nw(i)=0;
end;

do i=5 to 1 by -1
if rx(i) ne 0 then do;
nw(i) = 1;
leave;
end;
end;
run;

pp2014
Fluorite | Level 6

Thanks Reeza for the help. It worked..

novinosrin
Tourmaline | Level 20

two loops can be avoided:

data have;
input id rx1-rx5;
cards;
12 1 0 4 0 3
13 0 2 0 0 0
14 0 0 3 1 0
15 2 1 0 0 2
;

data want;
set have;
array t(*) rx:;
array nw(5);
do _n_=5 by -1 to 1;
nw(_n_)=0;
if flag then nw(_n_)=0;
else if t(_n_) ne 0 then do;nw(_n_)=1;flag=1;continue;end;
end;
drop flag;
run;
pp2014
Fluorite | Level 6

I am trying to use the above  code in macro as follows:

 

%macro test;
data testdata;
set lib.testdata;

array trx(106) trx1-trx106;
array nw(106) nw1- nw106;


%do i=1 %to 106;
nw(&i)=0;
%end;

%do i=106 %to 1 by -1;
%if trx(&i) ne 0 %then %do;
nw(&i) = 1;
leave;
%end;
%end;
run;
%mend;

%test;

 

I get the following error.

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: 1 by -1
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro TEST will stop executing.

 

 

I would appreciate any help in this..

Reeza
Super User

Don't write a macro loop, it doesn't add anything here at all, make it a regular DO loop. 

 


@pp2014 wrote:

I am trying to use the above  code in macro as follows:

 

%macro test;
data testdata;
set lib.testdata;

array trx(106) trx1-trx106;
array nw(106) nw1- nw106;


%do i=1 %to 106;
nw(&i)=0;
%end;

%do i=106 %to 1 by -1;
%if trx(&i) ne 0 %then %do;
nw(&i) = 1;
leave;
%end;
%end;
run;
%mend;

%test;

 

I get the following error.

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: 1 by -1
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro TEST will stop executing.

 

 

I would appreciate any help in this..


 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 838 views
  • 2 likes
  • 3 in conversation