BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Vibcom
Fluorite | Level 6

I want to show some specific obs from a dataset with Proc Print

Ex-- we have a dataset which contains name with different Genders.

Name     Gender

 

X            M

Y            M

Z            F

A            M

 

I want to print only Gender = M obs with Proc Print, we can use Where condition.But if i don't want to print all obs but some specific number (For Ex-2 -- only x and Y not Z)   

How to do it?

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Why not simply

proc print data=have(where=(gender='M') obs=2);
run;

or equivalently

proc print data=have(obs=2);
where gender='M'; 
run;

 

 

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18
data temp;
set have(where=(gender='M'));
run;
proc print data=temp (obs=2); var <list of variables to print>; run;
FreelanceReinh
Jade | Level 19

Why not simply

proc print data=have(where=(gender='M') obs=2);
run;

or equivalently

proc print data=have(obs=2);
where gender='M'; 
run;

 

 

andreas_lds
Jade | Level 19

Maybe you need to write down the rules an observation has to fulfil to be printed.

s_lassen
Meteorite | Level 14

If you want the first n observations of a specific subset, it is much easier to use SQL with the OUTOBS option:

Proc sql outoubs=20;
  select name from have where gender='M';
quit;

You may want to use ORDER BY to make sure you get the topmost observations, SQL does not guarantee the order otherwise.

SuryaKiran
Meteorite | Level 14

Are you trying to control the number of records to print or you want to print the list of record that you wish?

If you have a list where your print result need to include from then, Why not list them in where condition. Create a list of values into a macro and then later filter them in Proc print where condition.

 

proc sql noprint;
select quote(strip(name)) into:names separated by ','
from sashelp.class
where age>13;
quit;

proc print data=sashelp.class(where=(sex='M' and name in (&names.)));
run;

 

Thanks,
Suryakiran

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 8476 views
  • 0 likes
  • 6 in conversation