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?
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;
data temp;
set have(where=(gender='M'));
run;
proc print data=temp (obs=2);
var <list of variables to print>;
run;
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;
Maybe you need to write down the rules an observation has to fulfil to be printed.
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.
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;
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!
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.