Hi,
I dont know the number of observations available in a dataset. I want to create a new variable called TREATMENT and assign values 'Dummy 1' and 'Dummy 2' randomly!
25% of observations should assign with 'Dummy 1' and 75% with 'Dummy 2'.
Can you please suggest the sas code!
Thanks in advance.
This paper has exactly what you need
http://www2.sas.com/proceedings/sugi31/168-31.pdf
(I found it using the following google search
random selection site:sas.com
.)
Doc Muhlbaier
Duke
Is this helpful?
data class; /* find out the number of observations in your dataset */
set sashelp.class end=last nobs=n;
if last then
call symputx('nobs',n);
run;
data temp;
set class ;
r=ranuni(0);
proc sort;
by r;
run;
data want(drop=r);
length treatment $8;
set temp;
if _n_<=.25*&nobs then treatment='dummy1';
else treatment='dummy2';
run;
Linlin
A similar approach :
data design(keep=_trt);
call streaminit(6578765);
set sashelp.class nobs=nobs;
do _n_ = 1 to nobs;
_trt = rand("UNIFORM");
output;
end;
stop;
run;
proc rank data=design out=design percent; var _trt; run;
data want(drop=_trt);
set sashelp.class;
set design;
if _trt <= 25
then treatment = "Dummy 1";
else treatment = "Dummy 2";
run;
PG
And since there is always room for improvement :
data design(keep=_rnd treatment);
set sashelp.class nobs=nobs;
do _n_ = 1 to nobs;
_rnd = rand("UNIFORM");
treatment = ifc(_n_ <= 0.25*nobs, "Dummy 1", "Dummy 2");
output;
end;
stop;
run;
proc sort data=design; by _rnd; run;
data want;
merge sashelp.class design(drop=_rnd);
run;
PG
Even simpler, use surveyselect (dfficult to extend to more than two treatments, see Usage Note http://support.sas.com/kb/23/091.html ) :
proc surveyselect data=sashelp.class out=want outall rate=0.25;
run;
data want(drop=Selected);
set want;
treatment = ifc(Selected, "Dummy 1", "Dummy 2");
run;
PG
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!
Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.