create a binary matrix

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

create a binary matrix


So I need this help creating a binary matrxi, it's actually to convert a trips matrix to a binary matrix.

A part of the original matrix looks like this:

banana forestorange forestpear forest
3121
402
023

as I have for total 10,000 rows (farmers)* 20 columns (forests)

I hope to covnert it to something like this, (visited or not)

banana forestorange forestpear forest
111
101
011

Please let me kow how do I efficiently do the convertion.

Thanks a lot.


Accepted Solutions
Solution
‎01-25-2013 04:05 PM
Super Contributor
Posts: 1,636

Re: create a binary matrix

data have;

input forest1-forest20;

cards;

1 0 3 4 5 6 7 8 9 10 0 0 13 0 0 16 17 18 0 0

1 0 0 0 5 6 7 8 9 10 0 0 13 0 0 16 0 0 0 0

;

data want;

set have;

array _ff forest1-forest20;

do over _ff;

if _ff>0 then _ff=1;

end;

run;

proc print;run;

View solution in original post


All Replies
Solution
‎01-25-2013 04:05 PM
Super Contributor
Posts: 1,636

Re: create a binary matrix

data have;

input forest1-forest20;

cards;

1 0 3 4 5 6 7 8 9 10 0 0 13 0 0 16 17 18 0 0

1 0 0 0 5 6 7 8 9 10 0 0 13 0 0 16 0 0 0 0

;

data want;

set have;

array _ff forest1-forest20;

do over _ff;

if _ff>0 then _ff=1;

end;

run;

proc print;run;

Contributor
Posts: 46

Re: create a binary matrix

thanks a lot!! I appreciate!!

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 199 views
  • 0 likes
  • 2 in conversation