Indicator for year of sale

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Indicator for year of sale

[ Edited ]

Hi Community

 

I have the following dataset:

 

ID year event 

1  2014  0

1  2015  0

1  2016  1

2  2014  0

2  2015  1

2  2016  1

3  2014  1

3  2015  1

3  2016  1

 

I need to create an indicator that shows me the year the event happend, even for the years that the event was 0, as follows:

 

ID year event  year_event 

1  2014  0        2016

1  2015  0        2016

1  2016  1        2016

2  2014  0        2015

2  2015  1        2015

2  2016  1        2015

3  2014  1        2014

3  2015  1        2014

3  2016  1        2014

 

Any help is highly appreciated. Sorry if it is too simple.

 

Thanks!


Accepted Solutions
Solution
‎09-21-2017 03:57 PM
Respected Advisor
Posts: 2,161

Re: Indicator for year of sale

UNTESTED CODE

data first;
set have;
prev_event=lag(event);
prev_id=lag(id);
if id=prev_id and event=1 and prev_event=0 then output;
run;
data want;
merge have first(rename=(year=year_event) drop=prev_event prev_id);
by id;
run;

Assumes data is sorted by ID and year.

 

--
Paige Miller

View solution in original post


All Replies
Solution
‎09-21-2017 03:57 PM
Respected Advisor
Posts: 2,161

Re: Indicator for year of sale

UNTESTED CODE

data first;
set have;
prev_event=lag(event);
prev_id=lag(id);
if id=prev_id and event=1 and prev_event=0 then output;
run;
data want;
merge have first(rename=(year=year_event) drop=prev_event prev_id);
by id;
run;

Assumes data is sorted by ID and year.

 

--
Paige Miller
Senior User
Posts: 1

Re: Indicator for year of sale

Posted in reply to PaigeMiller

TESTED CODE:

 

data count(rename= Year=Year_event);
set records;
by ID;
if first.ID then count=0;
if event eq 1 then count+1;
if count eq 1 then output;
drop event;
run;

 

data result;
merge records count;
by ID;
drop count;
run;
proc print data=result;
run;

 

Super User
Posts: 10,313

Re: Indicator for year of sale

data have;
input ID year event ;
cards;
1  2014  0
1  2015  0
1  2016  1
2  2014  0
2  2015  1
2  2016  1
3  2014  1
3  2015  1
3  2016  1
;
run;
data want;
 do until(last.id);
   set have;
   by id;
   if event and not found then do;new_year=year;found=1;end;
 end;
 do until(last.id);
   set have;
   by id;
   output;
 end;
 drop found;
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 177 views
  • 2 likes
  • 4 in conversation