## Store total number of records in a variable and select random sample

Solved
Regular Contributor
Posts: 186

# Store total number of records in a variable and select random sample

Hi,

I would like to know how to store the number of records from a table in a variable and then based on that number select a random sample that is 10% of the size of the total number of records.

Here is what I came up with so far. My source table is cq_alert.closed_alert_cust :

data _NULL_;

call symput('nrows',trim(left(put(n,8.))));

stop;

run;

%put nobs= int(&nrows/10); /* I'm trying to store 10% of my total number of records in variable nobs */

To illustrate this better, if my source table had 100 records, I would like to create a table with 10 random records from it.

Thank you for your help and time.

Accepted Solutions
Solution
‎03-15-2013 12:12 PM
Posts: 3,852

## Re: Store total number of records in a variable and select random sample

Just use RATE=.1 with survelselect.

data h;
do i = 1 to 100;

output;

end;

run;
proc surveyselect data=h rate=.1 out=sample;
run;

All Replies
Solution
‎03-15-2013 12:12 PM
Posts: 3,852

## Re: Store total number of records in a variable and select random sample

Just use RATE=.1 with survelselect.

data h;
do i = 1 to 100;

output;

end;

run;
proc surveyselect data=h rate=.1 out=sample;
run;
Regular Contributor
Posts: 186

## Re: Store total number of records in a variable and select random sample

Thank you very much for the quick reply.

For the seconds part of my question :

Lets says I have this :

data _NULL_;

call symput('nrows',trim(left(put(n,8.))));

stop;

run;

%put nobs= int(&nrows/10);

How do I use the variable nobs in a normal data procedure?

Posts: 3,852

## Re: Store total number of records in a variable and select random sample

You could create a macro variable NOBS with %LET or you could create the variable in your data step and use it in code as &nobs.

%let nobs= %sysevalF(&nrows/10,INTEGER);
Regular Contributor
Posts: 186

## Re: Store total number of records in a variable and select random sample

Exactly what I needed.

Thank you very much for your help and time!

🔒 This topic is solved and locked.