BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Mluna
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

7 REPLIES 7
JackieJ_SAS
SAS Employee

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: 

SAS Help Center: %EVAL

SAS Help Center: %SYSEVALF

JackieJ_SAS
SAS Employee

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. 

Tom
Super User Tom
Super User

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

Mluna
Fluorite | Level 6
Thank you! I'm silly I knew they had to be integers, I knew i was supposed to round the answer because that's how i currently do it in excel.
ballardw
Super User

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.

Mluna
Fluorite | Level 6
Thank you for the tip! I'll follow this to include the log next time!. The formula evaluates to a specific number so Sampsize is what i wanted to use. It was just me forgetting to round the number back to a whole integer.
Mluna
Fluorite | Level 6
Thank you! I knew I was supposed to round it. I'm just very forgetful.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 551 views
  • 10 likes
  • 4 in conversation