BookmarkSubscribeRSS Feed
heba2000
Calcite | Level 5

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

12 REPLIES 12
LarryWorley
Fluorite | Level 6

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.

heba2000
Calcite | Level 5

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

LarryWorley
Fluorite | Level 6

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.

art297
Opal | Level 21

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;

heba2000
Calcite | Level 5

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

art297
Opal | Level 21

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

heba2000
Calcite | Level 5

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

art297
Opal | Level 21

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.

MikeZdeb
Rhodochrosite | Level 12

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;

heba2000
Calcite | Level 5

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.

art297
Opal | Level 21

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

heba2000
Calcite | Level 5

I got the data

Thank you all for your help

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 3791 views
  • 0 likes
  • 4 in conversation