Help using Base SAS procedures

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

Accepted Solution Solved
Reply
Regular Contributor
Posts: 186
Accepted Solution

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_;

  if 0 then set cq_alert.closed_alert_cust nobs=n;

  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
Respected Advisor
Posts: 3,777

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;

View solution in original post


All Replies
Solution
‎03-15-2013 12:12 PM
Respected Advisor
Posts: 3,777

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_;

  if 0 then set cq_alert.closed_alert_cust nobs=n;

  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?

Respected Advisor
Posts: 3,777

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 191 views
  • 3 likes
  • 2 in conversation