identify various observations for same variable

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

identify various observations for same variable

Hi SAS community,

I am wondering how I might be able to identify different observations under the same variable by creating a dummy variable

I want to create a dummy variable called initial_product to identify which was the first product customers chose before they change to another product.

 

The dataset I want to get looks like this ( I already have ID and product variables and observations), How do I create the variable initial_product ?

 

ID  product  Initial_product

1      A              Y

1      A              Y 

1      B              N

 

2     B              Y

2     C             N

2     C             N

 

3     A             Y

3     B             N

3     B             N

 

I would appreciate any comments or suggestions.

I tried lag function, first.id/last.id but none of this gets me what I want. Thanks


Accepted Solutions
Highlighted
Solution
‎12-12-2017 07:41 PM
PROC Star
Posts: 581

Re: identify various observations for same variable

[ Edited ]

data have;
input ID product $ ;
datalines;
1 A Y
1 A Y
1 B N
2 B Y
2 C N
2 C N
3 A Y
3 B N
3 B N
;
data want;
set have;
by id;
retain _t;
if first.id then
do;
_t=product;
initial_product='Y';
end;
else if product eq _t then initial_product='Y';
else initial_product='N';

drop _t;
run;

View solution in original post


All Replies
Highlighted
Solution
‎12-12-2017 07:41 PM
PROC Star
Posts: 581

Re: identify various observations for same variable

[ Edited ]

data have;
input ID product $ ;
datalines;
1 A Y
1 A Y
1 B N
2 B Y
2 C N
2 C N
3 A Y
3 B N
3 B N
;
data want;
set have;
by id;
retain _t;
if first.id then
do;
_t=product;
initial_product='Y';
end;
else if product eq _t then initial_product='Y';
else initial_product='N';

drop _t;
run;

New Contributor
Posts: 3

Re: identify various observations for same variable

Posted in reply to novinosrin
Thanks. Your code produced results exactly the way I wanted. Greatly
appreciate your help.
Super User
Posts: 11,759

Re: identify various observations for same variable


junep wrote:

I would appreciate any comments or suggestions.

I tried lag function, first.id/last.id but none of this gets me what I want. Thanks


Better for future questions be to show the approach you tried that got closest to what you wanted and explain why it did not meet your needs.

Sometimes you may only be one option or order of operations from a correct solution. Since I can see several First. approaches that would generate what you want I suspect you may have been pretty close and just missed one bit.

 

You might want to explicitly specify what happens if they switch back such as

 

4    B   Y

4     A  N

4    B   ?

 

 

Also as you do more coding you might want to consider binary numeric coding with 1 for Y and 0 for N instead of character values.

The Sum of a 1/0 coded variable over a group gives you the number of "yes" values. The mean would be the percent  "yes". And some procedures, regressions for instance, often require numeric results.

New Contributor
Posts: 3

Re: identify various observations for same variable

Thanks for your advice. I wasn't getting anywhere with my codes so I knew
my codes were wrong. But as you mentioned it would be good to know where I
made mistakes
☑ This topic is solved.

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

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