DATA Step, Macro, Functions and more

How to get a 10% of the population for each state

Reply
Contributor
Posts: 31

How to get a 10% of the population for each state

I have a business scenario where I have to select only 10% of the total observations for each state. for ex:

The final output should have 10 % of the population from each state. Please assist me with the code. I tried using Proc SurveySelect but did not work.

Respected Advisor
Posts: 3,124

Re: How to get a 10% of the population for each state

You would have to share more details about your problems, such as your code and the log involved. As it seems to me, proc surveyselect works as being expected. Please see the following example:

proc sort data=sashelp.class out=have;

by sex;

run;

Proc SurveySelect data=have out=want samprate=0.1;

strata sex;

run;

proc print;run;

Not sure any special method or algorithm  you are applying will make a difference, so more details will be needed.

Regards,

Haikuo

Contributor
Posts: 31

Re: How to get a 10% of the population for each state

Hi,

I have a business scenario where I have to select only 10% of the total observations for each state. for ex:

Statetotal observationsNo of obs to be output (10% of total)
Northeast435434355
Northwest123231233
Southeast87657887658
Southwest38748238749

The final output should have 10 % of the population from each state. Please assist me with the code. I tried using Proc SurveySelect but did not work.

PROC Star
Posts: 7,363

Re: How to get a 10% of the population for each state

Did you try the code proposed by Hai.Kuo?  Seems like it should work:

proc sort data=yourdatafilelibandname out=have;

  by state;

run;

Proc SurveySelect data=have out=want samprate=0.1;

  strata state;

run;

Super User
Posts: 10,521

How to get a 10% of the population for each state

Cherry wrote:

Hi,

I have a business scenario where I have to select only 10% of the total observations for each state. for ex:

Statetotal observationsNo of obs to be output (10% of total)
Northeast435434355
Northwest123231233
Southeast87657887658
Southwest38748238749

The final output should have 10 % of the population from each state. Please assist me with the code. I tried using Proc SurveySelect but did not work.

HOW did it not work? No output at all? Expected number of observations not selected? What did your code look like?

Super User
Super User
Posts: 6,502

Re: How to get a 10% of the population for each state

If you only want approximately 10% then just use a random number generator to give each obs 10% chance of being selected.

data want ;

   set have ;

   if ranuni(0) <= .10 then output;

run;

Proc SurveySelect should work for this problem.  Could you post the code you tried?

Trusted Advisor
Posts: 1,300

How to get a 10% of the population for each state

The following will not take a random sample but it will return every nth record from each by group.

proc sql;

create view class as

select *

from sashelp.cars

order by origin;

quit;

data want;

set class;

by origin;

array accum&sysindex[1] _temporary_; *temp so no need to drop later;

if first.origin then accum&sysindex[1]=10;

accum&sysindex[1]+1;

if accum&sysindex[1]>10 then

  do;

   accum&sysindex[1]=accum&sysindex[1]-10;

   output;

  end;

run;

proc sql;

select a.origin,total,output

   from (select origin,count(1) as total from sashelp.cars group by origin) a

  left join (select origin,count(1) as output from want group by origin) b on a.origin=b.origin;

quit;

Origintotaloutput
Asia15816
Europe12313
USA14715
Ask a Question
Discussion stats
  • 6 replies
  • 281 views
  • 0 likes
  • 6 in conversation