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.

Posts: 3,167

## 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:

 State total observations No of obs to be output (10% of total) Northeast 43543 4355 Northwest 12323 1233 Southeast 876578 87658 Southwest 387482 38749

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: 8,163

## 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: 13,498

## 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)Northeast435434355Northwest123231233Southeast87657887658Southwest38748238749The 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
Posts: 8,068

## 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?

Posts: 1,318

## 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
Discussion stats
• 6 replies
• 335 views
• 0 likes
• 6 in conversation