Random Samples

Reply
N/A
Posts: 0

Random Samples

I'm using Proc SurveySelect to select a simple random sample (Method=SRS) for each hospital. The sample size required is 25 or all (if 25 not available). The code works if there are at least 25 records in the pool for selection. It fails if there are fewer than 25 records. I've tried changing to Method=PPS, but I can't get the syntax right. Below is the code I'm currently using.
Any help is appreaciated.
SheriB

proc surveyselect data=HMASelect
n=25;
method=srs
out=HSW;
run;
Super Contributor
Posts: 260

Re: Random Samples

ISn't there a STRATA statement missing in your program ?
I thought that it would look something like

PROC SURVEYSELECT DATA = patients N = 25 METHOD = SRS OUT = sample ;
STRATA hospital ; /* provided that the data is already sorted by hospital */
RUN ;

And be careful, there is way too much semi-colons in your program !
N/A
Posts: 0

Re: Random Samples

I'm not using a strata statement because this is part of
a macro (see below). I loop through the macro once for each hospital. It works great if the hospital has enough patients to select the full random sample. If I need 25, but there are only 21 patients to select, then the code fails.

I need something that allows me the request 25 or all available.

Thanks for your input. I'm really stuck!
Sheri

%MACRO MSELECT(DSNIN,COMP,DSNOUT,SampSize,Frequency,Title2);

data HMASelect (drop=Company);
set &DSNIN;
if Company="&COMP";
run;

proc surveyselect data=HMASelect
n=&SampSize
method=srs
out=&DSNOUT;
run;


ODS PDF FILE="L:\Production\HMASamples\&DSNOUT..PDF";

proc print data=&DSNOUT split='*';
label Patient_Last_Name = 'Patient*Last*Name'
DischargeDate = 'Discharge*Date'
Patient_First_Name = 'Patient*First*Name';
var DCYrMonth AdvEncNo DischargeDate Patient_Last_Name Patient_First_Name;
format DischargeDate MMDDYY10.;
Title "&COMP - &Frequency Random Sample of Inpatient Encounters with an HMA Attending Physician";
title2 "&TITLE2";
FOOTNOTE "http://ftwweb07/HMFWOutcomes/HMA/SharedDocuments
";
run;
proc delete data=HMASelect;
run;

ODS PDF CLOSE;

%MEND MSELECT;


%MSELECT(Report1,HAR,HAR_MTH_Attending_Sample,25,Monthly,DVT - Lipi
SAS Super FREQ
Posts: 8,718

Re: Random Samples

Hi, Sheri:
One thing you can do in SAS Macro Language is test and change the value of macro variables, so INSIDE your macro program, you could do this:[pre]

data HMASelect (drop=Company);
set &DSNIN;
if Company="&COMP";
run;

*** new code starts;
proc sql;
select nobs into :numobs
from dictionary.tables
where libname="WORK" and
memname="HMASELECT";
quit;

%put before test numobs=&numobs sampsize=&sampsize;

%if &numobs lt &sampsize %then
%let sampsize = &numobs;

%put after test numobs= &numobs sampsize= &sampsize;
*** new code stops;


proc surveyselect data=HMASelect
n=&SampSize
method=srs
out=&DSNOUT;
run;
[/pre]
The dictionary.tables file is available to your SAS session as a tool for you to collect information about your datasets. So, the NOBS variable in dictionary.tables will contain the number of observations and the INTO puts the value of NOBS into a macro variable called &numobs. (BTW, the library names and member names in dictionary.tables are stored in all caps...so the WHERE clause will not work if you used lowercase or mixed case.)

Then, you can compare &numobs (the internally constructed macro variable) to &sampsize (the macro variable that you pass in) and do the adjustment accordingly.

cynthia
Occasional Contributor
Posts: 6

Re: Random Samples

Cynthia,

Thank you so much!! It worked like a charm. I haven't used Dictionary.tables before. I will definitely look into it to see what else I can use it for.

Are you with SAS?
Sheri
SAS Super FREQ
Posts: 8,718

Re: Random Samples

Sheri:
I'm so glad it worked. I absolutely LOVE the SAS Macro Facility -- it is very cool to be able to write self-adjusting programs.

Yes, I do work for SAS. I teach classes for the Education Division. :-) But in a previous job, as a programmer, I wrote lots and lots and lots of macro code. :-) really, LOTS....
cynthia
N/A
Posts: 0

Re: Random Samples

Sheri,

You could also try this:

******************************;
data HMASelect (drop=Company);
set &DSNIN;
if Company="&COMP";
run;
**new code starts;
data b;
retain obs_n;
if _N_=1 then obs_n=total;
set HMASelect nobs=total;
run;

proc sql noprint;
select case
when obs_n <25 then obs_n
else 25
end into :ss
from b;
quit;
**new code ends;
proc surveyselect data=HMASelect
method=srs out==&DSNOUT
n=&ss noprint;
run;
Occasional Contributor
Posts: 6

Re: Random Samples

PM,
Thanks for the second method to solve this problem. I tested and did get the second macro variable containing the number of records in the dataset. I would need to compare the two values and make sure the lowest value is used by the Proc Survey.

I've never seen a macro variable created in a Proc SQL.
Thanks for the insight.
Sheri
N/A
Posts: 0

Re: Random Samples

You might also try using the SELECTALL option within the PROC SURVEYSELECT statement.
Ask a Question
Discussion stats
  • 8 replies
  • 672 views
  • 0 likes
  • 4 in conversation