SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
San_rise
Fluorite | Level 6

Please help in validating this code. In 9.4 I am not able to get correct value of EXCLUDE in the output.

Seems like there is some error around 'and not missing'.

 

data test;

set test1;

array hra_ (*) &hras.;

array d_(&ct.);

 

do i=1 to &ct.;

if (((max(of hra_ (*)) = .  and min(of hra_(*)) = .)

and not missing(HRA_previous_yr))

or ((max(of hra_ (*)) = min(of hra_ (*))) and

not missing(HRA_previous_yr) and (HRA_previous_yr) = max((of hra_ (*))))

and intck('day', HRA_previous_yr, disenrollment_date) < 365

then do; EXCLUDE = "XYZ";

END;

END;

run;

8 REPLIES 8
Shmuel
Garnet | Level 18

You should provide some test data and the log of your run. 

What do you mean by  EXCLUDE = "XYZ"; ?

San_rise
Fluorite | Level 6

Hey

I am getting the output, but the value in the Variable EXCLUDE is not coming as "XYZ". Suspecting that the IF condition has some error.

EXCLUDE = "XYZ" is a command given to create variable EXCLUDE with value as "XYZ".

maguiremq
SAS Super FREQ
Please post example data. Use this site if you need to figure it out how to get your current data set into a DATA step.

https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/

Please also post your log. We can't tell what's going on without your log.
maguiremq
SAS Super FREQ

In addition to @Shmuel's suggestions, you should format your code by using the run symbol in the body tab in your post.

 

data test;

set test1;

array hra_ (*) &hras.;

array d_(&ct.);

 

do i=1 to &ct.;

if (((max(of hra_ (*)) = .  and min(of hra_(*)) = .)

and not missing(HRA_previous_yr))

or ((max(of hra_ (*)) = min(of hra_ (*))) and

not missing(HRA_previous_yr) and (HRA_previous_yr) = max((of hra_ (*))))

and intck('day', HRA_previous_yr, disenrollment_date) < 365

then do; EXCLUDE = "XYZ";

END;

END;

run;

Try to create some normal programming syntax pattern so that your code is readable. Indentation is important to break up various statements.

 

Where are your macro variables coming from (&ct., &hras.)? That's another question in itself.

San_rise
Fluorite | Level 6

Thank you for spacing the code. Here is the Log

San_rise_0-1641228615402.png

 

ballardw
Super User

First thing: What the heck is that do i= loop in there for? Your shown code would loop over the exact same values however many times the value of &Ct may be. One almost might guess that once upon a time this iterated over the values of one of the arrays testing each value but this code doesn't. You aren't referencing either of the arrays using i as an index.

 

Hint: If an array of numeric values returns missing, as in

max(of hra_ (*)) = .

the min will ALWAYS be missing as the only way the Max and Min functions return missing is when all values are missing.

So the bit of

and min(of hra_(*)) = .

is not needed and adds additional () that aren't actually needed after removing the min. Alternate code could also be to test if all values are missing.

nmiss(of hra_(*)) = dim(hra_a)

The bit of

max(of hra_ (*)) = min(of hra_ (*))

Could be replaced with

Range( of hra_(*))=0

Range is largest non-missing minus smallest non-missing. So when the max and min are the same the range is 0.

 

Note: You may also need to consider Leap years some where if that is why you are hard coding 365.

Tom
Super User Tom
Super User

If the code is unchanged and it used to work and now it doesn't then the DATA has changed.  Either the input dataset or the two input macro variables.

 

The code is confusing with extra parentheses that are not needed and it looks like you are missing some parentheses that are probably needed.  It looks like you have a logical expression in the form

A or B and C

Did you mean

(A or B) and C

or did you mean

A or (B and C)

Please explain in words what you condition is trying to detect.

Note to test if all of the values are missing just use the N( ) function. If N(of HRA_[*]) is zero then all of the variables are missing.

San_rise
Fluorite | Level 6

Condition 1: (max(HRA) = missing  and min(HRA) = missing) and HRA_prev_yr is not missing

 

OR

 

Condition 2: max(hra) = min(HRA) and HRA_prev_yr is not missing and HRA_prev_yr = max(HRA)

and intck('day',hra_prev_yr,disenrollment_date) <365

 

So it is A OR B.

 

Note: HRA here is macros with arrays ranging from 1 to 41 and is in date format.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1159 views
  • 1 like
  • 5 in conversation