DATA Step, Macro, Functions and more

Help in finding the New Rx Writers

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Help in finding the New Rx Writers

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


Accepted Solutions
Solution
‎01-16-2018 05:56 PM
Super User
Posts: 23,951

Re: Help in finding the New Rx Writers

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


All Replies
Super User
Posts: 23,951

Re: Help in finding the New Rx Writers

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

Solution
‎01-16-2018 05:56 PM
Super User
Posts: 23,951

Re: Help in finding the New Rx Writers

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;

Frequent Contributor
Posts: 131

Re: Help in finding the New Rx Writers

Thanks Reeza for the help. It worked..

Super User
Posts: 2,045

Re: Help in finding the New Rx Writers

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;
Frequent Contributor
Posts: 131

Re: Help in finding the New Rx Writers

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

Super User
Posts: 23,951

Re: Help in finding the New Rx Writers

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


 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 162 views
  • 2 likes
  • 3 in conversation