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
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;
Whats the logic/rules to get from rx to nw?
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;
Thanks Reeza for the help. It worked..
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;
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..
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..
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.