BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
junep
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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;

junep
Calcite | Level 5
Thanks. Your code produced results exactly the way I wanted. Greatly
appreciate your help.
ballardw
Super User

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.

junep
Calcite | Level 5
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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