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
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;
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 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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.