Help using Base SAS procedures

How Do I generate Random Numbers, but evenly between groups

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

How Do I generate Random Numbers, but evenly between groups

In my dataset from an actual clinical trial, I have 64 ID numbers (numbered 1 to 64), where 32 goes to treatment group A and 32 goes to treatment B and a score (outcome)

So I want to add a hypothetical variable where I want to generate a variable (let's call it Gender/Sex, so 2 levels)  that is sex is evenly and randomly distributed within each group (16 males and 16 females per treatment group). How do I do this randomly?


Accepted Solutions
Solution
‎05-01-2015 09:52 AM
Valued Guide
Posts: 858

Re: How Do I generate Random Numbers, but evenly between groups

If you use survey select you can name your strata:

proc surveyselect data='trial' method=srs n= 'sample size'

      seed= 'number' out='trial sample;

strata  "enter strata here, gender sex with no column, try both and see what gives you the desired output"

run;

I put everything you'll need to type in quotes.  Hope this helps.

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: How Do I generate Random Numbers, but evenly between groups

Well, that statisticians out there probably have the best answers for this.  My way would be:

data want;

     call streaminit(123);

     set have;

     male_cnt=0;

     female_cnt=0;

     if rand("uniform") <0.5 then do;

          if male_cnt <16 then do;

               sex="male";

               male_cnt=male_cnt+1;

          end;

          else do;

               sex="female";              

               female_cnt=female_cnt+1;

     end;

     else do;

          /* same as above but reveresed */

     end;

run;

Probably a bit simplistic and not so random, but if it doesn't matter too much...


Solution
‎05-01-2015 09:52 AM
Valued Guide
Posts: 858

Re: How Do I generate Random Numbers, but evenly between groups

If you use survey select you can name your strata:

proc surveyselect data='trial' method=srs n= 'sample size'

      seed= 'number' out='trial sample;

strata  "enter strata here, gender sex with no column, try both and see what gives you the desired output"

run;

I put everything you'll need to type in quotes.  Hope this helps.

Contributor
Posts: 58

Re: How Do I generate Random Numbers, but evenly between groups

thanks that worked for me. I used that to randomly select the men.. and then if the sex variable is missing, i pop recoded that to women.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 395 views
  • 0 likes
  • 3 in conversation