Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Generating binary variables

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 01-11-2012 03:36 PM
(3664 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;**

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I got the data

Thank you all for your help

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.

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