Main code with problem
proc print data=SASHELP.CLASS(firstobs=5 obs=15); where Sex='M'; run;
So the general meaning of the above program is that observations whose sex is 'Male' will starting reading from 5th record and stops at 15th record but instead SAS starts reading from 10th record and goes all the way to the end of the dataset up to 19th record. So, what is there I am missing here because I don't get the logic here at all?
SASHELP.CLASS dataset for reference
proc print data=sashelp.class; run;
https://documentation.sas.com/doc/en/lrcon/9.4/n1iok7aawea9zzn1aso4ulosip7l.htm
WHERE takes precedence over OBS= and FIRSTOBS=.
If you comment out "OBS" part like this:
proc print data=SASHELP.CLASS/*(firstobs=5 obs=15)*/;
where Sex='M';
run;
the result will be:
And in the original (not commented-out code) this is the moment where FIRSTOBS=5 starts working, that is why you get:
and since there are less then 15 obs selected by the WHERE clause, you get them all.
Bart
The PROC PRINT starts at the 5th male record and continues to the 15th male record (which doesn't exist).
@Nipun22 wrote:
Can you please elaborate?
What needs elaboration? What is not clear in my previous reply?
https://documentation.sas.com/doc/en/lrcon/9.4/n1iok7aawea9zzn1aso4ulosip7l.htm
WHERE takes precedence over OBS= and FIRSTOBS=.
If you comment out "OBS" part like this:
proc print data=SASHELP.CLASS/*(firstobs=5 obs=15)*/;
where Sex='M';
run;
the result will be:
And in the original (not commented-out code) this is the moment where FIRSTOBS=5 starts working, that is why you get:
and since there are less then 15 obs selected by the WHERE clause, you get them all.
Bart
yes, exactly.
try answering like this someday that will be more helpful
This type of question has actually been around for 40 years, ever since the WHERE statement was invented. Either interpretation seems reasonable:
In fact, originally SAS couldn't decide which interpretation to use. For a year or two, using WHERE when OBS/FIRSTOBS was in effect would generate an error message. Finally SAS decided that the second interpretation would be the more useful one.
that's quite interesting! thanks for telling
Although i was hesitating initially to ask questions related on simple concepts like where statement etc but it seems it was logical.
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!
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.