DATA Step, Macro, Functions and more

Retain and augment value basis criteria and event

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

Retain and augment value basis criteria and event

Hi

 

I am trying to work around a certain business problem where I need to flag certain event. I have tried coding but unable to come to any solution being a novice. Any help will be appreciated.

 

For the simplicity purposes, consider the data below. I have two fields ID and event with ID as identifier and event as specific event either a or b

 

Value to move and augment in set. for example - for first set of a ( which are 3 obs. for ID1) as 0

Value to change to 1 when event b occurs and remains for set in continuation

Value to change to 2 when event changes again and to a

Value to change to 3 when event change again to b

 

Value should initialize to 0 post 3 events in event a

 

Input:

ID event

1     a

1     a

1     a

1     b

1     b

1     a

1     a

1     b

1     a

1     a

2     a

2     b

2     a

2     a

2     b

 

Output:

ID event  Value

1     a        0

1     a        0

1     a        0

1     b        1

1     b        1

1     a        2

1     a        2

1     b        3

1     a        0

1     a        0

2     a        0

2     b        1

2     a        2

2     a        2

2     b        3

 

Thanks in Advance

 

 

 

 


Accepted Solutions
Solution
‎09-15-2016 01:47 AM
Super User
Posts: 5,099

Re: Retain and augment value basis criteria and event

Worth trying:

 

data want;

set have;

by id event notsorted;

if first.id then value=0;

else if first.event then value + 1;

if value=4 then value=0;

run;

View solution in original post


All Replies
Solution
‎09-15-2016 01:47 AM
Super User
Posts: 5,099

Re: Retain and augment value basis criteria and event

Worth trying:

 

data want;

set have;

by id event notsorted;

if first.id then value=0;

else if first.event then value + 1;

if value=4 then value=0;

run;

Frequent Contributor
Posts: 86

Re: Retain and augment value basis criteria and event

Thanks. The solution has worked.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 186 views
  • 0 likes
  • 2 in conversation