BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nicnad
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

4 REPLIES 4
data_null__
Jade | Level 19

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;
nicnad
Fluorite | Level 6

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?

data_null__
Jade | Level 19

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);
nicnad
Fluorite | Level 6

Exactly what I needed.

Thank you very much for your help and time!

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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