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.
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
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.
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;
Cherry wrote:
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.
HOW did it not work? No output at all? Expected number of observations not selected? What did your code look like?
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?
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;
Origin | total | output |
---|---|---|
Asia | 158 | 16 |
Europe | 123 | 13 |
USA | 147 | 15 |
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.