So I'm trying to create a sample using SAS. I'm using a Sample Size formula defined below. But I get errors saying that the Eval has characters instead of numbers but I dont think it's true, also one of the other errors are in my options for the proc Surveyselect it says they don't belong? but I think they do, based on the Proc Surveyselect resources. any help would be appreciated.
Here is the Code, I'm using the Pg2 library from the 2nd lesson:
%let Z = 1.96;
%let P = 0.05;
%let E = 0.05;
proc sql noprint;
select count(*) into :N from pg2.class_birthdate;
quit;
%let Sample_Size = %eval((&N * ((&E * (1 - &E)) / (&P / &Z) ** 2)) / (((&E * (1 - &E)) / ((&P / &Z) ** 2)) + &N - 1));
%put Sample Size: &Sample_Size;
data SampledClass;
proc surveyselect data=pg2.class_birthdate
method=srs
out=SampledClass
sampsize= &sample_size
seed=1234;
run;
proc print data=SampledClass;
run;
In which case it might be better to use %SYSFUNC(CEIL()) instead of %SYSEVALF().
%let Z = 1.96; %let P = 0.05; %let E = 0.05; %let N=12345 ; %let Sample_Size=%sysfunc(ceil((&N * ((&E * (1 - &E)) / (&P / &Z) ** 2)) / (((&E * (1 - &E)) / ((&P / &Z) ** 2)) + &N - 1))); %put Sample Size: &Sample_Size;
Result
1590 %put Sample Size: &Sample_Size; Sample Size: 73
Try using the %SYSEVALF macro function instead of the %EVAL macro function in your statement beginning with %LET SAMPLE_SIZE. The %EVAL function works only with integer values and will throw an error if you give it an argument that includes decimal point characters. The %SYSEVALF function does floating point arithmetic, so works with numbers with decimal points. See the SAS documentation for more information:
Additionally, I see your problem with the PROC SURVEYSELECT code. The formula in your %LET SAMPLE_SIZE statement resolves to a non-integer. (I get 15.2413617261025). The SAMPSIZE= option on the PROC SURVEYSELECT statement requires an integer, so throws an error when you provide a non-integer.
In which case it might be better to use %SYSFUNC(CEIL()) instead of %SYSEVALF().
%let Z = 1.96; %let P = 0.05; %let E = 0.05; %let N=12345 ; %let Sample_Size=%sysfunc(ceil((&N * ((&E * (1 - &E)) / (&P / &Z) ** 2)) / (((&E * (1 - &E)) / ((&P / &Z) ** 2)) + &N - 1))); %put Sample Size: &Sample_Size;
Result
1590 %put Sample Size: &Sample_Size; Sample Size: 73
With @JackieJ_SAS's comment, perhaps you want the SAMPRATE , as in a percentage of observations, instead of SAMPSIZE, a specific number of observations?
Note: any time you get errors best practice on this forum is to copy the LOG text with the submitted code and all notes, messages, warnings or errors. Then on the forum open a text box and paste the copied log text.
The text box will preserve the text layout and position of diagnostic information SAS often supplies in error messages as the main message windows will reformat pasted text reducing the usefulness of the text somewhat.
Also the complete log text is important. We often have people ask about an "error" that is not indeed an error but a warning or sometimes just information.
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!