BookmarkSubscribeRSS Feed
Riteshdell
Quartz | Level 8

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

10 REPLIES 10
art297
Opal | Level 21

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

Riteshdell
Quartz | Level 8

 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.

 

 

 

art297
Opal | Level 21

If your file contains observation numbers you could use the where option when specifying your dataset.

 

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Riteshdell
Quartz | Level 8
I didn't understand your Where Line statement, Can you Please describe it .
mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

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;

art297
Opal | Level 21

@Shmuel: I don't believe _n_ is available outside of a datastep.

 

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

And even in a data step, it can't be used in a where statement.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nehalsanghvi
Pyrite | Level 9

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;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 10 replies
  • 3589 views
  • 2 likes
  • 5 in conversation