turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- identify various observations for same variable

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-12-2017 05:12 PM

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

Solution

12-12-2017
07:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to junep

12-12-2017 05:23 PM - edited 12-12-2017 05:27 PM

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;

All Replies

Solution

12-12-2017
07:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to junep

12-12-2017 05:23 PM - edited 12-12-2017 05:27 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to novinosrin

12-12-2017 07:42 PM

Thanks. Your code produced results exactly the way I wanted. Greatly

appreciate your help.

appreciate your help.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to junep

12-12-2017 05:43 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

12-12-2017 07:50 PM

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

my codes were wrong. But as you mentioned it would be good to know where I

made mistakes