BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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;
8 REPLIES 8
Olivier
Pyrite | Level 9
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 !
deleted_user
Not applicable
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
Cynthia_sas
SAS Super FREQ
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
Sheri
Calcite | Level 5
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
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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;
Sheri
Calcite | Level 5
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
deleted_user
Not applicable
You might also try using the SELECTALL option within the PROC SURVEYSELECT statement.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 8 replies
  • 2128 views
  • 0 likes
  • 4 in conversation