I understand using firstobs=x, and obs=x, to print out all variables for that observations. I am trying to use proc print for several observations that are far from each other in observation numbers. So, for example, if I want observations number 38701, I would do
proc print data=dataset (firstobs=38701, obs=38701).
Would anyone know how to print two or three observations that have observations numbers like 38701, 14080, and/or 9631. Thank you for your time in advance.
@kelSAS It's a very interesting question, at-least to me. I wonder why there isn't an option to filter obs within a proc print step. We can concur with the suggestions using 2 step solution.
I would like something like where obs in (1,5,8,...........n) ;
when we know the following works,, I would vote for your idea to have some obs or n within proc print to filter using random lists
data w;
set sashelp.class;
n+1;
run;
proc print data=w;
where n in (1,5,9);
run;
I am keen to see an opinion of the sas artist and king @art297 and @data_null__ respectively if they have the time 🙂 Thank you!
Do you mean filter a list (in (38701,14080,9631) in a proc print?
Maybe a view?
data myView / view=myView;
set sashelp.class;
if _n_ in (13, 12, 4);
run;
proc means data=myView;
run;
Yes! I want to see only those observations printed.
If you don't want to run multiple PROC PRINTs, you would first need to construct a data set holding the desired observations. For example:
data want;
do k=38701, 14080, 9631;
set have point=k;
output;
end;
stop;
run;
proc print data=want;
run;
Be sure to include that STOP statement. Without it, the top DATA step turns into an infinite loop.
@kelSAS It's a very interesting question, at-least to me. I wonder why there isn't an option to filter obs within a proc print step. We can concur with the suggestions using 2 step solution.
I would like something like where obs in (1,5,8,...........n) ;
when we know the following works,, I would vote for your idea to have some obs or n within proc print to filter using random lists
data w;
set sashelp.class;
n+1;
run;
proc print data=w;
where n in (1,5,9);
run;
I am keen to see an opinion of the sas artist and king @art297 and @data_null__ respectively if they have the time 🙂 Thank you!
I think @Astounding has the right idea using point. This will pull the OBS in the order specified. I would add a variable to ID the OBS number. If a data set REMOVED observations you would need to check _ERROR_ and handle appropriately.
data obsV / view=obsV;
length _OBS_ 8;
do point=1000,456,354;
set sashelp.heart point=point;
_obs_ = point;
output;
end;
stop;
run;
proc print n width=min;
run;
Thank you so much for your time. Much appreciate it.
Thank you so much for your active input. I wish there was one step, but your two-step works perfectly.
@novinosrin: I don't have anything to add to what has already been suggested other than I would create and store a macro (e.g., %printn) if this is a task that one has to repeat. E.g., using @data_null__'s suggested code:
%macro printn(dsn,obs);
%let obs = %sysfunc(tranwrd(%quote(&obs.),%str( ),%str(,)));
data obsV / view=obsV;
length _OBS_ 8;
do point=&obs.;
set &dsn point=point;
_obs_ = point;
output;
end;
stop;
run;
proc print n width=min;
run;
proc datasets memtype=view nodetails nolist;
delete obsV;
run;
%mend printn;
%printn(sashelp.heart,1000 456 354)
Art, CEO, AnalystFinder.com
@art297 Thank you sir for your time. It feels very satisfying to receive thoughtful responses from my most favorite people that I mentioned 2 here +Ksharp(didn;t bother this time as I assumed sleep time in china can be awkward).
Anyways, the point is although this seems pretty basic in essence to have a more than one step solution and then do a proc print. I kinda really felt intrigued OP's perspective of having this non sequential filter facility enabled within a proc print. (aka IN operator using the obs column)
I encountered a similar situation at my work before I quit to pursue Grad school on a requirement to do list pull for eligibility customers on a waterfall method. Back then I didn't pay much attention. But When i do envision the requirement becoming increasingly common, that facility could be a better utility. -- >my 2 cents.
@kelSAS wrote:
I understand using firstobs=x, and obs=x, to print out all variables for that observations. I am trying to use proc print for several observations that are far from each other in observation numbers. So, for example, if I want observations number 38701, I would do
proc print data=dataset (firstobs=38701, obs=38701).
Would anyone know how to print two or three observations that have observations numbers like 38701, 14080, and/or 9631. Thank you for your time in advance.
I would ask how do you know that you want observations 38701, 14080 and 9631?
And what happens if the data is sorted or the number of records changes? The observation numbers would or could change.
I would suggest turning the rule(s) you use to say that you need specific observations into syntax that can use a WHERE statement such as here:
proc print data=sashelp.class; Where (sex='F' and age=13) or name='John'; run;
OR add the observation number as a variable, obsnum for example, and use a WHERE statement to reference that variable such as:
where obsnum in (38701, 14080, 9631);
@ballardw Sir, I think OP is intuitively after your obsnum or my demo in one proc print step. If my understanding is right, OP perhaps wants that to happen in one print step.
Thank you for asking that question. I need to handle outliers properly and when I run 'proc univariate', the output would show the maximum and minimum with observation number and the value of the variable I ran 'proc univariate' with. I could use certain value to limit data that's proc print'ed, but was wondering if I could print several datalines by observation number in one proc print step.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.