BookmarkSubscribeRSS Feed
Elliott
Obsidian | Level 7

Why does SAS not evaluate the Or statment here?  How do I recode this so it will work?  The macro variables all resolve correctly.

IF WEEKDAY(&WOD.) = 1 THEN DO;

  IF ((listname NOT IN (&SUNLIST.))

       OR (listtype NOT IN (&LISTALL.)))

THEN DO;

ERROR_DESC = 'ERR_WORKLIST'; err1 = 1;

END;

ELSE do;

ERROR_DESC = 'NON_ERROR'; err1 = 0;

END;

END;

Thanks,

10 REPLIES 10
Astounding
PROC Star

While your conditions seem logical, they're not how SAS wants to see them.  (In fact, I couldn't even hazard a guess as to what SAS will do with them.)  A couple of changes probably fixes that:

IF NOT ((listname IN (&SUNLIST.)) 

       AND (listtype IN (&LISTALL.)))

THEN DO;

That should take care of it.

Good luck.

Reeza
Super User

Your code looks correct, but it may not be the logic you want.

I think you have to provide a sample of the logic you're after to help debug this. Sample input/output is preferable.

PGStats
Opal | Level 21

A simplified version of your code works just fine. The problem must be with the value of the macro variables...

1    data _null_;

2    do listname = "A", "B";

3        do listtype = 1, 2;

4            IF ((listname NOT IN ("A"))

5                   OR (listtype NOT IN (1)))

6            THEN DO;

7                put listname listtype 'ERR_WORKLIST';

8                END;

9            ELSE do;

10               put listname listtype 'NON_ERROR';

11               END;

12           end;

13       end;

14   run;

A 1 NON_ERROR

A 2 ERR_WORKLIST

B 1 ERR_WORKLIST

B 2 ERR_WORKLIST

PG

PG
ballardw
Super User

What are the values of your sunlist and listall variables?

And what exactly do you mean by "Does not evaluate"?

Does this give you any clues?


%let sunlist = "Junk"  "Crap";
%let listall = "num" "char";
data junk;
input listname $ listtype $;
IF ((listname NOT IN (&SUNLIST.))
       OR (listtype NOT IN (&LISTALL.)))
THEN DO;
ERROR_DESC = 'ERR_WORKLIST'; err1 = 1;
END;
ELSE do;
ERROR_DESC = 'NON_ERROR'; err1 = 0;
END;

datalines;
junk num
Junk Num
Junk num
JUNK NUM
crap num
;
run;

Elliott
Obsidian | Level 7

This is what I am trying to do.

&sunlist is a long list of report names, &listall has just one value at this time.  I am comparing the value in the listname field I am reading in from a csv file to the hardcoded list in &sunlist.  So the error would be if the listname value is not = to the values in &sunlist or not equal to the value in &listall.

There is a different list for each day.

it does not appear that the or part of the if statement  is working.

Thanks,

ballardw
Super User

Give an EXPLICIT example of how it is "not working". The exact value of the macro variables involved, including any quotes,. the exact values of some of the listname and listtype variables compared, the actual results and the expected results.

I think a very likely cause, especially since you say a "hardcoded list" is case sensitivity.

"Word" is not equal to "word" or "WORD" due to case of the letters involved. "Report Name" is not equal to "Report  Name" due to extra space, and leading or trailing blanks can also cause things not to be equal.

Did you get the code to "work" before creating macro variables?

PGStats
Opal | Level 21

You say "the error would be if the listname value is not = to the values in &sunlist or not equal to the value in &listall" but it is listtype that you compare to &listall...

PG

PG
Tom
Super User Tom
Super User

Given this explanation you can probably simplify your expression to a single IN () operator.

%let sunlist='A','B','C';

%let listall='D' ;

data ... ;

  if listname not in (&sunlist,&listall) then ...

  ...

run;

You also need to worry about the case of LISTNAME and &sunlist and &listall.  As coded in your example and mine the values must match exactly.

  if upcase(listname) not in (%upcase(&sunlist,&listall)) then ...

You also need to worry about the format for the values of SUNLIST and LISTALL macro variables that they could be properly used in the value list for the IN () operator.

ballardw
Super User

Also if your sunlist is defined as "reportname1 reportname2 reportname3" the comparison will fail because the text "reportname1" is not equal to the entire list.

BurntDirt
Calcite | Level 5

From looking at your code and reading your description of the problem, I don't think your code is doing what you are trying to do.

     "So the error would be if the listname value is not = to the values in &sunlist or not equal to the value in &listall"

I think you are trying to do this,

If ((listname NOT IN (&SUNLIST.)) AND (listtype NOT IN (&LISTALL.)))

One way to check your logic is to negate it and flip the then else.

From your description it sounds like you want to do this:

IF WEEKDAY(&WOD.) = 1 THEN DO;

  IF listname IN (&SUNLIST.)
       OR listtype IN (&LISTALL.)

THEN DO;
ERROR_DESC = 'NON_ERROR'; err1 = 0;
END;

ELSE do;
ERROR_DESC = 'ERR_WORKLIST'; err1 = 1;
END;

To create what I think you want from the code above, 1) negate the conditions, 2) change ORs to ANDs and ANDs to ORs and 3) flip the then else.

So, the code as you present the problem should be

IF WEEKDAY(&WOD.) = 1 THEN DO;

  IF listname NOT IN (&SUNLIST.)
       AND listtype NOT IN (&LISTALL.)

THEN DO;
ERROR_DESC = 'ERR_WORKLIST'; err1 = 1;

END;

ELSE do;
ERROR_DESC = 'NON_ERROR'; err1 = 0;

END;

Gary

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 784 views
  • 1 like
  • 7 in conversation