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

Hello everyone, I have these data and I want to find the item that recurring next year

the description of what I need : I want to create a dummy variable for the item, if the item in year+1 is in the year put 0, if it is not put 1. in summary, 1 for nonrecurring and 0 for recurring.

 

The data inputs

 

Code       Year     item

0000012017AB
0000012017AD
0000012017BC
0000012018AD
0000012018CC
0000012019CC
0000012019BC
0000022016FF
0000022016GG
0000022016DD
0000022017FF
0000022017GG
0000022017HH
0000022018HH
0000022018BB
0000022018GG
0000022019BB
0000022019GG
0000022019FF

 

The output that I need 

 

Code       Year     item    recurring

0000012017AB1
0000012017AD1
0000012017BC1
0000012018AD0
0000012018CC1
0000012019CC0
0000012019BC1
0000022016FF1
0000022016GG1
0000022016DD1
0000022017FF0
0000022017GG0
0000022017HH1
0000022018HH0
0000022018BB1
0000022018GG0
0000022019BB0
0000022019GG0
0000022019FF1

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input Code  $     Year     item $;
cards;
000001	2017	AB
000001	2017	AD
000001	2017	BC
000001	2018	AD
000001	2018	CC
000001	2019	CC
000001	2019	BC
000002	2016	FF
000002	2016	GG
000002	2016	DD
000002	2017	FF
000002	2017	GG
000002	2017	HH
000002	2018	HH
000002	2018	BB
000002	2018	GG
000002	2019	BB
000002	2019	GG
000002	2019	FF
;

proc sql;
 create table need as
 select a.*	,min(a.year)=a.year	or a.item ne b.item as recur
 from have a left join have b
 on a.code=b.code and a.year-1=b.year
 and a.item=b.item
 group by a.code
 order by  a.code,a.year;
quit;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data have;
input Code  $     Year     item $;
cards;
000001	2017	AB
000001	2017	AD
000001	2017	BC
000001	2018	AD
000001	2018	CC
000001	2019	CC
000001	2019	BC
000002	2016	FF
000002	2016	GG
000002	2016	DD
000002	2017	FF
000002	2017	GG
000002	2017	HH
000002	2018	HH
000002	2018	BB
000002	2018	GG
000002	2019	BB
000002	2019	GG
000002	2019	FF
;

proc sql;
 create table need as
 select a.*	,min(a.year)=a.year	or a.item ne b.item as recur
 from have a left join have b
 on a.code=b.code and a.year-1=b.year
 and a.item=b.item
 group by a.code
 order by  a.code,a.year;
quit;
FreelanceReinh
Jade | Level 19

Hello @EmadNoureldeen,

Glad to see that @novinosrin's solution worked for you. Then it would be fair and help later readers if you marked his helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select the post containing the working code as the solution after clicking "Not the Solution" in the option menu (see icon below) of the current solution.
show_option_menu.png

novinosrin
Tourmaline | Level 20


data have;
input Code  $     Year     item $;
cards;
000001	2017	AB
000001	2017	AD
000001	2017	BC
000001	2018	AD
000001	2018	CC
000001	2019	CC
000001	2019	BC
000002	2016	FF
000002	2016	GG
000002	2016	DD
000002	2017	FF
000002	2017	GG
000002	2017	HH
000002	2018	HH
000002	2018	BB
000002	2018	GG
000002	2019	BB
000002	2019	GG
000002	2019	FF
;

data want ;
 if _n_ = 1 then do ;
  dcl hash h() ;
  h.definekey ("item","year") ;
  h.definedone () ;
 end ; 
 do  until(last.code);
  do until(last.year);
   set have;
   by code year;
   h.add();
   _iorc_=h.check(key:item,key:year-1) ;
   recur=  _iorc_ ne 0;
   output;
  end;
  _iorc_=h.remove(key:item,key:year-1);
 end;
 h.clear();
run;
Ksharp
Super User
data have;
input Code  $     Year     item $;
cards;
000001	2017	AB
000001	2017	AD
000001	2017	BC
000001	2018	AD
000001	2018	CC
000001	2019	CC
000001	2019	BC
000002	2016	FF
000002	2016	GG
000002	2016	DD
000002	2017	FF
000002	2017	GG
000002	2017	HH
000002	2018	HH
000002	2018	BB
000002	2018	GG
000002	2019	BB
000002	2019	GG
000002	2019	FF
;
data want;
 if _n_=1 then do;
   declare hash h(dataset:'have',hashexp:20);
   h.definekey(all:'y');
   h.definedone();
 end;
set have;
recurring=(h.check(key:code,key:year-1,key:item) ne 0);
run;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1286 views
  • 2 likes
  • 4 in conversation