BookmarkSubscribeRSS Feed
cwarfield
Calcite | Level 5

After running this code, I am seeing all basins, not just NA or na. What am I doing wrong? It just doesn't make sense to me that  this would be ignored and there is no error to help me see why.

 

***********************************************************;
* Activity 3.06 *;
* 1) Modify the OUT= option in the PROC SORT statement *;
* to create a temporary table named STORM_SORT. *;
* 2) Complete the WHERE and BY statements to answer *;
* the following question: Which storm in the North *;
* Atlantic basin (NA or na) had the strongest *;
* MaxWindMPH? *;
***********************************************************;

proc sort data=pg1.storm_summary out=storm_sort;
where Basin = 'NA' or 'na';
by descending MaxWindMPH;
run;

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Your syntax is wrong.

where Basin = 'NA' or 'na';

should be

where Basin = 'NA' or basin='na';

From now on, when your code isn't working, show us the LOG so we can see the code along with NOTEs, ERRORs and WARNINGs that appear in the log.

--
Paige Miller
cwarfield
Calcite | Level 5
I've added the log, there are no warnings or errors. Basin is a character value, it is NA, na, SP, etc. There are a couple other options. All of them show in the output data so the where statement is being ignored.
 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc sort data=pg1.storm_summary out=storm_sort;
74 where Basin='NA' or 'na';
75 by descending MaxWindMPH;
76 run;
 
NOTE: There were 3118 observations read from the data set PG1.STORM_SUMMARY.
WHERE 1 /* an obviously TRUE WHERE clause */ ;
NOTE: The data set WORK.STORM_SORT has 3118 observations and 12 variables.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 2517.15k
OS Memory 32060.00k
Timestamp 04/18/2021 06:12:26 PM
Step Count 83 Switch Count 5
Page Faults 0
Page Reclaims 296
Page Swaps 0
Voluntary Context Switches 23
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 784
 
 
77
78 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
90
 
cwarfield
Calcite | Level 5

Thank you. I see now that the variable has to be listed again for the boolean operator to apply for a where statement.

ballardw
Super User

Another approach when comparing a single variable to a list of values is to use the IN operator:

 

Where basin in ('NA' 'na');

which is the same as multiple 'or'.

Another considering the difference between the values listed to make the case consistent:

 

Where upcase (basin) = 'NA';

Which makes the value compared to the list all upper case. If you actually have other case differences like "nA" or "Na" this might be preferred.

Manal02
Calcite | Level 5

You need to whrite the code this way :

proc sort data=pg1.storm_summary out=storm_sort;
where Basin in("NA" "na");
by descending MaxWindMPH;
run;

or you can just modify the where instructions as: where Basin= 'NA' or Basin= 'na'

 

Tom
Super User Tom
Super User

Because the non empty string 'na' is considered TRUE by SAS.

So your where condition reduces to WHERE (test1) or TRUE.  Which is always going to be TRUE.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1585 views
  • 5 likes
  • 5 in conversation