## How to Proc Print non-consecutive observations?

Frequent Contributor
Posts: 100

# How to Proc Print non-consecutive observations?

Greetings:
Just learned how to use Proc Univariate to see which observations have extreme values. The next step would be to actually take a look at those obs.

How do I say “proc print not the whole dataset but only a specific list of observations: e.g. where observation numbers in (1,2,5)”?

Tried to find an answer online but failed. My clumsy alternative now is to use a data step to select the observations (using _n_ as a way to select the observations) then proc print this new dataset. See example below.

Data ob2Select;
set obtest;
where _n_ in (1 2 5);
Run;
proc print data=ob2select;
Run;
Posts: 3,852

## Re: How to Proc Print non-consecutive observations?

Selects all rows some may be selected more than once.

[pre]
ods trace on;
ods select "Extreme Observations";
proc univariate data=sashelp.class;
ods output "Extreme Observations"=EXT;
run;
ods trace off;
data extV / view=extV;
set ext;
do point=lowobs,highobs;
set sashelp.class point=point;
_obs_ = point;
output;
end;
run;

proc print;
run;
[/pre]
Frequent Contributor
Posts: 100

## Re: How to Proc Print non-consecutive observations?

Thank you. Interesting solution. Will file this one to study more later (a bit over my level now). Wish there is a simple way to select by obs numbers directly in proc print...
PROC Star
Posts: 8,164

## Re: How to Proc Print non-consecutive observations?

Directly? I don't think so. Plus, I don't think you can capture _n_ directly with a where statement.

Indirectly, you could write and then repeatedly call a small macro that does what your original workaround was intended to do. For example:

%macro procprnt (filename,criteria);
data _forprint;
set &filename.;
if &criteria.;
run;

proc print data=_forprint;
run;
%mend;

%procprnt(sashelp.class,_n_ in (1 3 5 7))
Super Contributor
Posts: 3,176

## Re: How to Proc Print non-consecutive observations?

Reply to OP: As shown by data _null_ with his example, essentially you have the opportunity to use "observation_number" when generating a SAS VIEW from your original input file based on your data selection criteria.

Scott Barry
SBBWorks, Inc.

data class / view=class;
set sashelp.class;
* optionally, some WHERE condition goes here. ;
obsnum = _n_;
run;
proc print u data=class;
where obsnum in (1,5,7,10);
run;
Valued Guide
Posts: 2,191

## Re: How to Proc Print non-consecutive observations?

SET statement allows direct access by observation number to data in base sas libraries, so
data the_parts ;
do obs_wanted = 1,3,5,9 ;
set yourlib.data_set point= obs_wanted ;
output ;
end
stop ; *<-------<<<<< inportant;
run ;
proc print ;
run ;
Frequent Contributor
Posts: 100

## Re: How to Proc Print non-consecutive observations?

Thanks to all of you! I really did not expect anyone spending a Sunday moment to address my beginner question :-). Now I’m feeling less miserable about having to learn SAS in my spare time.
Valued Guide
Posts: 653

## Re: How to Proc Print non-consecutive observations?

We are all learning.
A variation on the UNIVARIATE extremes can be obtained with the IDGROUP option in SUMMARY. The resulting data set looks much like the one obtained through ODS and UNIVARIATE. The DATA step then transposes it.
[pre]proc summary data=sashelp.class;
var height;
output out=max5
idgroup(max(height) obs out[5] (name sex age height)=)/autoname;
run;

data fiveobs(keep=obsnum name sex age height);
set max5;
array Aobs {5} _obs_:;
array Aname {5} name:;
array Asex {5} sex:;
array Aage {5} age:;
array Ahght {5} height:;
do i = 1 to 5;
obsnum=aobs{i};
name = aname{i};
sex = asex{i};
age = aage{i};
height=ahght{i};
output fiveobs;
end;
run;

proc print data=fiveobs;
run;[/pre]
Frequent Contributor
Posts: 100

## Re: How to Proc Print non-consecutive observations?

Tried this. Neat. Can't wait to learn SAS arrays (4 lessons away)!
Thank you for sharing the tip and the encouragement!
Discussion stats
• 8 replies
• 244 views
• 0 likes
• 6 in conversation