04-22-2018 09:44 PM
I have a macro do loop I am running and I want the flag to be applied as soon as the condition is met. Right now it is only applying it to the last variable in the loop. Diag1-Diag24 are diagnosis codes that I am looking through to find the value of the code I need to flag.
%do s=1 %to &dxnum. ;
if ("T36" <=substr(diag&s.,1,3)<="T50") then flag=1 ;
This works but it only picks up the values in diag24. I feel like I need to do a do until or a 'leave' but I've tried a dozen different ways with no luck. I need flag=1 whenever it meets that condition in any of the variables diag1-diag24. Once it flags it once it can leave the loop.
04-22-2018 10:07 PM
Any particular reason to not be using arrays here?
Transposing to a long format is also a much easier solution.
array dx(&dxnum) diag1-diag&dxnum.; flag=0; do i=1 to &dxnum. ; if ( 36 <= input(substr(dx(i), 2,2), 8.) <= 50 ) then do;
flag=1 ; leave;
04-23-2018 11:11 AM
A couple of general comments/questions on that macro.
First you have S as a parameter but then force an assignment to S in a do loop. So why is S a parameter?
Understanding that this is a development in progress but using macro variables inside a macro not explicitly passed into the macro has many potential for bad behaviors.
Since you are setting a single variable FLAG to 1 then testing multiple variables will likely lead to hard to tell which variable met the condition.
And probably most critical: use of <= (or >= or just > and < with strings is seldom appropriate please see:
data _null_; diag1 = 'T4 '; if ("T36" <=substr(diag1,1,3)<="T50") then flag=1; put diag1= flag=; run;
Obviously we do not have every possible value of your diagxx variables but consideration for string values with likely numeric comparison needed is always problematic.
Example input and desired output is often helpful. And sometimes how that result is to be further used.
You could probably describe the needed behavior with only 4 or 5 DIAG variables.
The question about "leave" would require information about do you want to stop for the first match (flag)? Or do you need to know for each DIAG variable? (Not leave but ARRAY processing).