DATA Step, Macro, Functions and more

How can I print random number observations in Data set?

Reply
Contributor
Posts: 68

How can I print random number observations in Data set?

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

PROC Star
Posts: 7,467

Re: How can I print random number observations in Data set?

Posted in reply to Ritesh_dellvostro

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

Contributor
Posts: 68

Re: How can I print random number observations in Data set?

 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.

 

 

 

PROC Star
Posts: 7,467

Re: How can I print random number observations in Data set?

Posted in reply to Ritesh_dellvostro

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

 

Art, CEO, AnalystFinder.com

 

Trusted Advisor
Posts: 1,018

Re: How can I print random number observations in Data set?

[ Edited ]
Posted in reply to Ritesh_dellvostro

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

 

Contributor
Posts: 68

Re: How can I print random number observations in Data set?

I didn't understand your Where Line statement, Can you Please describe it .
Trusted Advisor
Posts: 1,018

Re: How can I print random number observations in Data set?

[ Edited ]
Posted in reply to Ritesh_dellvostro

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

Trusted Advisor
Posts: 1,554

Re: How can I print random number observations in Data set?

Posted in reply to Ritesh_dellvostro

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;

PROC Star
Posts: 7,467

Re: How can I print random number observations in Data set?

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

 

Art, CEO, AnalystFinder.com

 

Trusted Advisor
Posts: 1,018

Re: How can I print random number observations in Data set?

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

 

Frequent Contributor
Posts: 75

Re: How can I print random number observations in Data set?

[ Edited ]
Posted in reply to Ritesh_dellvostro

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;

 

Ask a Question
Discussion stats
  • 10 replies
  • 158 views
  • 0 likes
  • 5 in conversation