10-23-2017 04:54 PM - edited 10-23-2017 05:05 PM
I am struggling stratifying my sample that has a predefined bound in SAS.
Say I have a dataset named "PopSamp" with many sample points and different prices like below:
Sample Points Price
Now suppose I have a dataset named "StrataBounds" with my strata upper bounds as follows:
Strata Upper Bound
May someone help me figure out how I can write a code that will use my "PopSamp" dataset and add a variable strata using the "StrataBounds" dataset? This would be done by seeing in what strata the price is found.
My final dataset should look like this:
Sample Points Price Strata
A .87 2
B 1.97 3
C .27 1
D 2.27 3
E 4.97 4
F .17 1
G 2.42 3
H .03 1
I 5.99 4
J 3.69 3
10-23-2017 06:33 PM
What have you tried so far? While you can do this in a single statement, why not start out with a series of
IF .. THEN ... ;
ELSE IF .. THEN ..
10-23-2017 06:48 PM
This would work great if I had just one survey to do however, I have a lot of surveys to do and many of them have different number of strata. I will need to write a proc sql I think.
10-23-2017 07:22 PM
Then show us a dataset of strata boundaries, along with corresponding survey id's.
No doubt this can be done in PROC SQL, but it can also be done in a DATA step. To show how, we need to know what the boundaries dataset looks like. Preferably presented as a SAS dataset, with INPUT statements and boundary values.
10-23-2017 07:47 PM
If the "strata" was in the sample at the time of collection (it should have been by the way) where did it go?
Does your "strata" variable as resolved need to be numeric or character?
If character works you can use Proc format to add them assuming you know your actual bounds:
proc format library=work; value Strata_one 0- .42 = '1' .42< - 1.50 = '2' 1.50<- 3.99 = '3' 3.99<- 999999='4'; run; data have; set want; strata = put(price,strata_one.); run;
This is more efficient if any of the "strata" in different sets use the same rule. Create as many as needed.
Formats can be made from data sets relatively easily for this if the bounds are in a data set.
10-25-2017 07:39 AM
One possibility is to use PROC SQL to create a SELECT statement (or rather, the WHEN and OTHERWISE statements that go inside it) in a macro variable:
data PopSamp; input Point $ Price; cards; A .87 B 1.97 C .27 D 2.27 E 4.97 F .17 G 2.42 H .03 I 5.99 J 3.69 ;run; data StrataBounds; input stratum upper_bound; cards; 1 0.42 2 1.50 3 3.99 4 999999 ;run;
proc sql noprint; select case when upper_bound<999999 then cats('when(price<',upper_bound,') stratum=',stratum) else cats('otherwise stratum=',stratum) end into :select_stmt separated by ';' from StrataBounds order by upper_bound; quit; data want; set PopSamp; select; &select_stmt; end; run;