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

My data is like:

idyeargvkeyindicator
1201110000
1201210001
1201520000
2201010000
2201030000
2201330001
2201320000
3

2017

20000
3201820001
2201830000

 

Group by id and year, I want the new_indicator to depend on the indicator's value. As long as the indicator has a 0 in an id's particular year, the new_indicator of this id becomes 1 in that year, regardless of how many 0 or 1 that id has for the indicator in the year.

 

So, the intended outcome should be:

idyeargvkeyindicatornew_indicator
12011100001
12012100010
12015200001
22010100001
22010300001
22013300011
22013200001
3

2017

200001
32018200011
32018300001

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile datalines expandtabs;
input id  year gvkey  indicator;
datalines;
1	2011	1000	0
1	2012	1000	1
1	2015	2000	0
2	2010	1000	0
2	2010	3000	0
2	2013	3000	1
2	2013	2000	0
3	2017	2000	0
3	2018	2000	1
3	2018	3000	0
;

proc sql;
create table want as
select *,min(indicator)=0 as new_indicator
 from have
  group by id,year;
quit;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

See this:

data have;
infile datalines dlm="09"x;
input id $ year gvkey $ indicator;
datalines;
1	2011	1000	0
1	2012	1000	1
1	2015	2000	0
2	2010	1000	0
2	2010	3000	0
2	2013	3000	1
2	2013	2000	0
3	2017	2000	0
3	2018	2000	1
3	2018	3000	0
;

data want;
merge
  have
  have (
    keep=id year indicator
    rename=(indicator=_ind)
    where=(_ind = 0)
    in=zero
  )
;
by id year;
new_indicator = zero;
drop _ind;
run;

Please post your data in the future in a data step with datalines like I did, so we don't have to do that for you. It also prevents any misunderstandings about raw content, variable types and attributes.

Ksharp
Super User
data have;
infile datalines expandtabs;
input id  year gvkey  indicator;
datalines;
1	2011	1000	0
1	2012	1000	1
1	2015	2000	0
2	2010	1000	0
2	2010	3000	0
2	2013	3000	1
2	2013	2000	0
3	2017	2000	0
3	2018	2000	1
3	2018	3000	0
;

proc sql;
create table want as
select *,min(indicator)=0 as new_indicator
 from have
  group by id,year;
quit;
andreas_lds
Jade | Level 19

And another solution:

 

data work.want;
   set work.have;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.have(keep= id year indicator where= (indicator=0))');
      h.defineKey('id', 'year');
      h.defineDone();
   end;

   new_indicator = h.check() = 0;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1026 views
  • 1 like
  • 4 in conversation