Hello Techiez,
I am looking for some solution where I can print some selected random number of observation of data set.
suppose a Data set contains 100 observation and i want to print some random observation like 8,25,47,56 observation.
How can we do it.
Can any one help me on it.
thanks in advance.
Regards,
Ritesh
You can always select then print them. e.g.:
data temp; set sashelp.class; if _n_ in (5,8,15); run; proc print; run;
Art, CEO, AnalystFinder.com
Thanks Art297, I understood your solution and it helped me to understand it.
but If I want to print without creating any other dataset, I mean in existing dataset.
Is there any print option is available.
Many thanks for your solution.
If your file contains observation numbers you could use the where option when specifying your dataset.
Art, CEO, AnalystFinder.com
I didn't know this couild work (becuase the where statement has no reference to any data set variables), but your question made me test it. And it does work:
proc print data=sashelp.class;
where ranuni(267892343)<=.5;
run;
PS: It's random but not a predetermined subset
The WHERE statement is a filter to tell SAS to keep only those observations that qualify.
So I could have said
where sex='F';
to get only the females,
or even use a function in the where statement:
where sqrt(height/age)>.75;
to get tall students.
Instead I used another function: RANUNI (generates a uniformly random variate between 0 and 1). By constraining it to be less than 0.5 I will get approximately half the cases.
Edit addtion: not just "half the cases" but a quasi-random half.
Another edit addition: Using the IFN function, you can even do differing sampling fractions based on the value of data variables:
proc print data=sashelp.class;
where ranuni(10598150)< ifn(sex='M',.8,.3);
run;
iw
differing samplings
If you know ahead what observations yo want to print, you may try next code:
proc print data=<dataset_name> (where=(_N_ in (5,25,40,18));
var ... ;
run;
And even in a data step, it can't be used in a where statement.
If you have SAS/STAT, this is another option:
proc surveyselect data=yourdata method=srs n=5 out=sample;
run;
proc print data=Sample;
run;
If you are using only Base SAS, this is another way, change the seed in the ranuni function to get a different sort order each time:
proc sql;
create table randomorder as
select *
from yourdata
order by ranuni(5678);
quit;
proc print data=randomorder (obs=3);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.