BookmarkSubscribeRSS Feed
LOLO
Obsidian | Level 7

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.

 

%let dxnum=24;

%macro test(s);

%do s=1 %to &dxnum. ;

if  ("T36" <=substr(diag&s.,1,3)<="T50") then flag=1 ;

%end;

%mend test;

%test

 

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.

 

Any ideas???

 

 

 

 

2 REPLIES 2
Reeza
Super User

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;
end;
end;
ballardw
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 419 views
  • 0 likes
  • 3 in conversation