Help using Base SAS procedures

Randomization

Reply
New Contributor
Posts: 2

Randomization

Hi Everyone,

Please help me with the below randomization.

3 types of injections (A, B and C) are randomised to 3 locations in each arm (L1 L2 L3 R1 R2 R3). 

L and R corresponds to LEFT AND RIGHT arms respectively.

For each arm, A has to be given at least once, remaining two locations in each arm can be allocated to either B or C.

It can both be B. Also It can both be C or It can be B and C.


I use PROC PLAN to generate random numbers and writing it to a SAS dataset.


Thanks in advanced.


This is what I got to so far.

proc plan seed=1832577;

  factors a=20 b=6;

  output out = c;

run;

%macro calc;

data dat(keep = SubjectNr Location arm Treatment allocatedarm allocatedarm2 allocatedarm3 allocTr1 allocTr2 allocTr3);

  length Treatment $8.;

  length Location $2.;

  length arm $5;

  set c;

  retain ct ctt allocatedarm allocatedarm2 allocatedarm3 allocTr1 allocTr2 allocTr3;

  if ct = . or ct ne a then do; 

  ct = a; ctt = 0; end;

  ctt = ctt + 1;

      if b<= 3 then do;

      arm='right';

      end;

      else

      arm='left';

    if ctt=1 then do;

      treatment = 'A';

allocatedarm=arm;

allocTr1= treatment;

      end;

     

  if ctt=2 then do;

        if arm ne allocatedarm then do;

treatment = 'A'; end;

else do;

treatment = 'B or C';

            end;

            allocatedarm2=arm;

            allocTr2 = treatment;

  end;

 

  if ctt=3 then do;

      if (allocTr1 eq allocTr2) or (allocatedarm eq allocatedarm2) then

            treatment = 'B or C';

      if (allocatedarm eq allocatedarm2) and (arm ne allocatedarm) then

            treatment = 'A';

      allocatedarm3=arm;

      allocTr3 = treatment;

  end;

  if ctt=4 then do;

      if (arm ne allocatedarm) and (allocatedarm2 ne allocatedarm) and (allocatedarm3 ne allocatedarm) then

      treatment = 'A';

  end;

SubjectNr = a;

location = b;

run;

/* proc sort data = dat out = dat; by SubjectNr; run; */

ods rtf file = " .rtf";

footnote "";

proc print data = dat noobs;

var SubjectNr Location Treatment arm allocatedarm allocatedarm2 allocatedarm3 allocTr1 allocTr2 allocTr3;

run;

ods rtf close;

%mend;

%calc;

quit;

Respected Advisor
Posts: 3,899

Re: Randomization

If I understood your requirements right then below code should do what you're after.

proc plan seed=1832577;

  factors Subj=20 Loc=6;

  output out = Plan;

run;

proc format;

  value arm

    1-3 ='Right'

    4-6 ='Left'

    ;

  picture Location

    1-3 = '00000000' (prefix='R')

    4-6 = '00000000' (prefix='L')

    ;

run;

data _null_;

  set Plan end=last;

  SubjectNr=put(Subj,z10.);

  Location=put(Loc,Location2.);

  Arm=put(Loc,arm5.);

  length Treatment $8;

  if _n_=1 then

    do;

      dcl hash h1(multidata:'y',ordered:'yes');

      _rc=h1.defineKey('SubjectNr','Arm','Treatment');

      _rc=h1.defineData('SubjectNr','Arm','Treatment','Location');

      _rc=h1.defineDone();

    end;

  Treatment='A';

  if h1.check() ne 0 then

    do;

      _rc=h1.add();

    end;

  else

    do;

      if ceil(ranuni(1832577)*2)=1 then Treatment='B';

      else Treatment='C';

      _rc=h1.add();

    end;

  if last then

    do;

      _rc=h1.output(dataset:'want');

    end;

run;

New Contributor
Posts: 2

Re: Randomization

Thanks Patrick for this

Super Contributor
Posts: 426

Re: Randomization

May I request you to tell me how ranuni works here as i confused with seeds'1832577'?

ceil(ranuni(1832577)*2

New Contributor
Posts: 3

Re: Randomization

Ranuni returns a random number from the uniform distribution between 0 and 1.  So it looks like here, the phrase

ceil(ranuni(1832577)*2)

will return 0 when the random number generated is 0,

1 when the random number generated is greater than 0 and less than or equal to .5,

and 2 when the random number generated is greater than .5 and less than or equal to 1.

The seed is specified here just to give a reproducible result.  If you do not specify a seed, the results will change each time the code is run.  The seed can be any number less than 2^31-1, including a date or time value as those are stored as numeric values in SAS.

Ask a Question
Discussion stats
  • 4 replies
  • 357 views
  • 0 likes
  • 4 in conversation