BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nipun22
Obsidian | Level 7

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;

Nipun22_1-1728566000485.pngNipun22_2-1728566015150.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Maxim #1: Read the doc.

 

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:

yabwon_0-1728567364293.png

And in the original (not commented-out code) this is the moment where FIRSTOBS=5 starts working, that is why you get:

yabwon_1-1728567470554.png

and since there are less then 15 obs selected by the WHERE clause, you get them all.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

The PROC PRINT starts at the 5th male record and continues to the 15th male record (which doesn't exist).

--
Paige Miller
Nipun22
Obsidian | Level 7
Can you please elaborate?
PaigeMiller
Diamond | Level 26

@Nipun22 wrote:
Can you please elaborate?

What needs elaboration? What is not clear in my previous reply?

--
Paige Miller
yabwon
Onyx | Level 15

Maxim #1: Read the doc.

 

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:

yabwon_0-1728567364293.png

And in the original (not commented-out code) this is the moment where FIRSTOBS=5 starts working, that is why you get:

yabwon_1-1728567470554.png

and since there are less then 15 obs selected by the WHERE clause, you get them all.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Nipun22
Obsidian | Level 7
So it means that SAS will create a subset of the dataset using the where statement and then it will run firstobs and obs.
Also if the subset of the main dataset had more than 15 records then it would have stopped reading on 15th record? right?
yabwon
Onyx | Level 15

yes, exactly.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Nipun22
Obsidian | Level 7

@PaigeMiller 

try answering like this someday that will be more helpful

Astounding
PROC Star

This type of question has actually been around for 40 years, ever since the WHERE statement was invented.  Either interpretation seems reasonable:

  1. Consider observations 5 through 15, and select only the males
  2. Consider only the males, and of those take the 5th through the 15th

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.

Nipun22
Obsidian | Level 7

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.

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!

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
  • 9 replies
  • 229 views
  • 2 likes
  • 4 in conversation