Desktop productivity for business analysts and programmers

Creating binary variables for unique IDs

New Contributor
Posts: 2

Creating binary variables for unique IDs

Hello! I have the following question.

I am using SAS Enterprise Guide and I have the data structured like that:

Client ID:    Product:

1                    A

1                    B

2                    A

2                    D

2                    C

3                    B

What I am trying to do is create binary variables for every product for every customer ID so that I can built a predictive model in SAS Enterprise Miner.

I want the data to look like that:

Client ID:    Product A:        Product B:         Product C:   Product D:

1                     1                      1                         0             0

2                      1                      0                         1             1

3                      0                      1                         0              0

The problem is that there are over 50 different products, and each client has different number of products and the ordinary functions are not working. Is there any other way to create that table with a query?


Super User
Posts: 9,865

Re: Creating binary variables for unique IDs

Code: Program

data have;
input ClientID   Product $;
1 A
1 B
2 A
2 D
2 C
3 B
data have;
set have;
retain v 1;
proc transpose data=have out=temp(drop=_:) prefix=Product_;
by ClientID;
var v;
id Product;
proc stdize data=temp out=want missing=0 reponly;run;

New Contributor
Posts: 2

Re: Creating binary variables for unique IDs

Thank you! Is there a way how to do it without listing the variables and the data, because I have 50 000 rows of observations?

Respected Advisor
Posts: 4,811

Re: Creating binary variables for unique IDs

The first step in Xia's code is only there to create some example data. Use your own dataset instead. You could start with the second step as :

data have;

set myLibrary.myDataset;

retain v 1;



Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation