Randomly assigning Dummy Treatments

Reply
New Contributor
Posts: 2

Randomly assigning Dummy Treatments

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.

Valued Guide
Posts: 2,106

Re: Randomly assigning Dummy Treatments

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

Super Contributor
Posts: 1,636

Re: Randomly assigning Dummy Treatments

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

Respected Advisor
Posts: 4,606

Re: Randomly assigning Dummy Treatments

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

PG
Respected Advisor
Posts: 4,606

Re: Randomly assigning Dummy Treatments

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

PG
Respected Advisor
Posts: 4,606

Re: Randomly assigning Dummy Treatments

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

PG
Post a Question
Discussion Stats
  • 5 replies
  • 2968 views
  • 1 like
  • 4 in conversation