BookmarkSubscribeRSS Feed
learner123
Calcite | Level 5

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.

5 REPLIES 5
Doc_Duke
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

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

PGStats
Opal | Level 21

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
PGStats
Opal | Level 21

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
PGStats
Opal | Level 21

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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 5 replies
  • 6169 views
  • 1 like
  • 4 in conversation