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

Showing results for

Find a Community

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-11-2012 03:36 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 03:46 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to LarryWorley

01-11-2012 03:50 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 04:04 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 04:11 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

01-11-2012 04:32 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 04:42 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

01-11-2012 05:03 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 05:08 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 05:09 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MikeZdeb

01-11-2012 05:32 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to heba2000

01-11-2012 05:33 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

01-11-2012 06:00 PM

I got the data

Thank you all for your help