Help using Base SAS procedures

Generating binary variables

Reply
Occasional Contributor
Posts: 12

Generating binary variables

I need to generate 5 binary variables but  each observation must take 1 in one variable and zero in the other 4 variables.I need the code for this case

Can anyone help in this case

Thanks in advance

Frequent Contributor
Posts: 129

Generating binary variables

Within data step:

Set all five binary variables to zero;

Then use select construct or if-then else construct or other appropriate strategy to assign 1 to the appropriate binary variable.

Hope that helps.

Occasional Contributor
Posts: 12

Generating binary variables

but in this case only the first two variables will be zero and 1 but the other 3 all zeros because the condition for the second variable would be =0 if var1=1 and =1 if var1=0 so I canot use this

Frequent Contributor
Posts: 129

Generating binary variables

I am assuming you have an underlying variable with 5 levels or categories, then you could try this:

Assuming a pre-existing variable to generate the binary variables from:

Suppose x can be equal to 1, 2, 3, 4 or 5.  Then do something like this:

b1 = 0 ;

b2 = 0 ;

b3 = 0 ;

b4 = 0;

b5 = 0;

Select (x) ;

  when (1) b1 = 1 ;

  when (2) b2 = 1 ;

  when (3) b3 = 1 ;

  when (4) b4 = 1 ;

  when (5) b5 = 1;

end ;

so you will end up with data set like this:

x   b1 b2 b3 b4 b5

1   1   0   0  0   0

2   0  1    0  0   0

3   0   0   1  0   0

4   0   0   0  1   0

5   0   0   0  0   1

If instead you want the binary variables based on line numbers, you could do some like this:

if mod(_n_,5) = 0 then x = 5 ;

else x = mod(_n_,5) ;

then add code from above.

If this does not do it, I am really confused about what you are trying to do.

PROC Star
Posts: 7,357

Generating binary variables

Or, if you need to randomly assign which b variable gets a one, you could use something like:

data want (drop=i pop);

  array b(5);

  do pop=1 to 1000;

    do i=1 to 5;

      b(i)=0;

    end;

    b(int(5*ranuni(0))+1)=1;

    output;

  end;

run;

Occasional Contributor
Posts: 12

Generating binary variables

I have problem in generating x with values 1 to 5. how can I generate it?

PROC Star
Posts: 7,357

Generating binary variables

You have to explain what you mean.  int(5*ranuni(0))+1  would generate a number between 1 and 5.

Occasional Contributor
Posts: 12

Generating binary variables

I mean I wrote

proc iml;

x=j(60,5,.);

x=int(5*ranuni(0))+1;

but only one observation is generated and I need 60 observations with 5 variables

PROC Star
Posts: 7,357

Generating binary variables

I'm not familiar enough with proc iml to help you.  In the datastep I showed, above, changing the value of

  do pop=1 to 1000;

to

  do pop=1 to 60;

would give you 60 records with 5 variables.

Valued Guide
Posts: 765

Re: Generating binary variables

hi ... not IML, just a couple data steps ...

* 60 observations, x ranges from 1 to 5;

data x;

do _n_ = 1 to 60;

    x = ceil(5*ranuni(0));

          output;

end;

run;

* add binary variables based on value of x;

data x;

array b(5) (5*0);

set x;

b(x) = 1;

output;

b(x) = 0;

run;

Occasional Contributor
Posts: 12

Generating binary variables

I am not really familiar with data generation and when I just copied your code it was accepted but the data was not showed to me it is clear that i am missing one command to get the data produced.

PROC Star
Posts: 7,357

Generating binary variables

From both Mike's and my code, the data files would be shown in your work directory.

Occasional Contributor
Posts: 12

Generating binary variables

I got the data

Thank you all for your help

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