BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
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;
8 REPLIES 8
data_null__
Jade | Level 19
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]
mnew
Calcite | Level 5
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...
art297
Opal | Level 21
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))
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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;
Peter_C
Rhodochrosite | Level 12
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 ;
mnew
Calcite | Level 5
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.
ArtC
Rhodochrosite | Level 12
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]
mnew
Calcite | Level 5
Tried this. Neat. Can't wait to learn SAS arrays (4 lessons away)!
Thank you for sharing the tip and the encouragement!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 8 replies
  • 981 views
  • 0 likes
  • 6 in conversation