DATA Step, Macro, Functions and more

Categorize

Accepted Solution Solved
Reply
Super Contributor
Posts: 673
Accepted Solution

Categorize

  A product can have three categories 'X','Y','Z'.
   Now if a customer has all three product X,Y,Z or X Y or X Z then he should be categorized as having X
     if he has Y or Z then he shouuld be categorized as Y
       if only Z then categoried as having only Z
      
Customer    product     category
A                      X            X
A                      Y        
A                      Z
B                      Y            Y
B                      Z
C                     X            X
C                     Z       
D                     Z            Z


Accepted Solutions
Solution
‎04-17-2015 11:41 AM
Valued Guide
Posts: 860

Re: Categorize

I think this is what you are looking for but without a question it's hard to tell:

data have;

infile cards dsd;

input Customer $ product $;

cards;

A,X

A,Y

A,Z

B,Y

B,Z

C,X

C,Z

D,Z

;

run;

proc sql;

create table want as

select *,min(product) as category

from have

group by customer;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,913

Re: Categorize

What is your question?

Trusted Advisor
Posts: 1,137

Re: Categorize

Please try

data have;

input Customer$    product$     ;

cards;

A                      X    

A                      Y    

A                      Z

B                      Y    

B                      Z

C                     X     

C                     Z     

D                     Z     

;

proc transpose data=have out=want;

by customer;

var product;

run;

data want2;

merge have want;

by customer;

if first.customer and cats(of colSmiley Happy in ('XYZ','XY','XZ') then category='X';

if first.customer and cats(of colSmiley Happy='YZ' then category='Y';

if first.customer and cats(of colSmiley Happy='Z' then category='Z';

drop _name_ col:;

run;

Thanks,

Jag

Thanks,
Jag
Solution
‎04-17-2015 11:41 AM
Valued Guide
Posts: 860

Re: Categorize

I think this is what you are looking for but without a question it's hard to tell:

data have;

infile cards dsd;

input Customer $ product $;

cards;

A,X

A,Y

A,Z

B,Y

B,Z

C,X

C,Z

D,Z

;

run;

proc sql;

create table want as

select *,min(product) as category

from have

group by customer;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 264 views
  • 5 likes
  • 4 in conversation