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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

New Learning Events in April

 

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.

LEARN MORE

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