BookmarkSubscribeRSS Feed
deleted_user
Not applicable
hi all,

I have a dataset with multiple subjects, and each subjects have multiple entries. Now I need to select a subset of subjects and all entries of each selected subject will be kept in my output.
Eg, I have the 'subject' variable in the original dataset look like:
1111 2 2 2 3 3 4444 555555
If I need to get 2 random subjects and their corresponding entries in the output file, say subject 1 and 4, then the output file should have the 'subject' variable with
1111 4444.

How can I achieve it in SAS? Thanks!
4 REPLIES 4
deleted_user
Not applicable
You can use ranuni(0) function to generate a random number and then POINT= option in a SET statement.
data random(keep =subject); sampsize=2; do i=1 to sampsize; pickit=ceil(ranuni(11111)*totobs); set work.table point=pickit nobs=totobs; output; end; stop; run;
Patrick
Opal | Level 21
Ignore this post. Message was edited by: Patrick
Patrick
Opal | Level 21
First reorganise your data in a way that you have one subject per observation.

The following code then creates a sample with a given number of observations, selects all observations with the same likelyhood and doesn't select twice the same observation. The code is as provided by SAS.

data work.rsubset(drop=obsleft sampsize);
sampsize=10;
obsleft=totobs;
do while(sampsize>0);
pickit+1;
if ranuni(0) lt sampsize / obsleft then do;
set sasuser.revenue point=pickit
nobs=totobs;
output;
sampsize=sampsize-1;
end;
obsleft=obsleft-1;
end;
stop;
run;

Vasile: Your code could pick the same observation more than once (i.e. ranuni returns once 0.8... and once 0.7....).

HTH
Patrick Message was edited by: Patrick
deleted_user
Not applicable
Patrick, your observation is correct, thanks.

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
  • 4 replies
  • 1154 views
  • 0 likes
  • 2 in conversation