BookmarkSubscribeRSS Feed
cool1993
Fluorite | Level 6

Hello everyone,

 

My dataset is the following:

 

PersonPerson_numYearFirstyearCompany
A120051aa
A120060aa
A120070aa
A120080aa
A120080bb
A120090aa
A120090bb
B220081cc
B220090dd
B220090cc
B220100dd
B220110dd
B220120dd
C320021ee
C320021ff
C320030ee
C320030ff
C320040ff
C320080ff
C320090ff
C320100ff
C320110ff
C320120ff
C320120gg
C320130ff
C320140ff
D420051hh
D420060hh
D420100ii
D420110ii
D420120ii
D420140jj
D420150jj

 

The expected outcome is the following:

 

PersonPerson_numYearFirstyearCompanySaveWant
A120051aaaa1
A120060aaaa0
A120070aaaa0
A120080aaaa0
A120080bbaa0
A120090aaaa0
A120090bbaa0
B220081cccc1
B220090ddcc0
B220090cccc0
B220100dddd1
B220110dddd0
B220120dddd0
C320021ee{ee, ff}1
C320021ff{ee, ff}1
C320030ee{ee, ff}0
C320030ff{ee, ff}0
C320040ffff0
C320080ffff0
C320090ffff0
C320100ffff0
C320110ffff0
C320120ffff0
C320120ggff0
C320130ffff0
C320140ffff0
D420051hhhh1
D420060hhhh0
D420100iiii1
D420110iiii0
D420120iiii0
D420140jjjj1
D420150jjjj0

 

The logic behind is the following:

 

1. If firstyear = 1 then want = 1; 

2. If firstyear = 1, save = {companies}. That is, save include all companies when firstyear = 1. (Is it possible to save multiple variables into the save variable?)

3. If firstyear ne 0 and company = lag(save), then save = lag(save) and want = 0;

    else if the company in save then save = lag(save) and want = 0;

    else if, among the same year observations, there is an observation whose company is equal to the current observation's save variable, then save = lag(save) and want = 0;

    else save = code and want = 1;

 

My attempt is the following:

 

data have;
input Person $ Person_num Year Firstyear Company $;
datalines;
A	1	2005	1	aa
A	1	2006	0	aa
A	1	2007	0	aa
A	1	2008	0	aa
A	1	2008	0	bb
A	1	2009	0	aa
A	1	2009	0	bb
B	2	2008	1	cc
B	2	2009	0	dd
B	2	2009	0	cc
B	2	2010	0	dd
B	2	2011	0	dd
B	2	2012	0	dd
C	3	2002	1	ee
C	3	2002	1	ff
C	3	2003	0	ee
C	3	2003	0	ff
C	3	2004	0	ff
C	3	2008	0	ff
C	3	2009	0	ff
C	3	2010	0	ff
C	3	2011	0	ff
C	3	2012	0	ff
C	3	2012	0	gg
C	3	2013	0	ff
C	3	2014	0	ff
D	4	2005	1	hh
D	4	2006	0	hh
D	4	2010	0	ii
D	4	2011	0	ii
D	4	2012	0	ii
D	4	2014	0	jj
D	4	2015	0	jj
;
data want; set have;
do i = 1 to 4;
if person_num = i then do;
 	if firstyear = 1 then want = 1 and save = code; *How can assign multiple variables into the save variable when Person = C;
	if firstyear ne 0 and company = lag(save) then save = lag(save) and want = 0;
	else if company in save then save = lag(save) and want = 0;
	else if; *I have no idea in this part.;
	else want = 1;
end;
end;
run;

 

4 REPLIES 4
cool1993
Fluorite | Level 6

Hello,

 

I am stuck to solve the difficulty.

 

I need your knowledge..

 

The dataset and the expected result("Want variable" is the following:

 

PersonYearCompanyWant
A2005aa1
A2006aa0
A2007aa0
A2008aa0
A2008bb0
A2009aa0
A2009bb0
B2008cc1
B2009dd0
B2009cc0
B2010dd1
B2011dd0
B2012dd0
C2002ee1
C2002ff1
C2003ee0
C2003ff0
C2004ff0
C2008ff0
C2009ff0
C2010ff0
C2011ff0
C2012ff0
C2012gg0
C2013ff0
C2014ff0
D2005hh1
D2006hh0
D2010ii1
D2011ii0
D2012ii0
D2014jj1
D2015jj0

 

The logic to determine the "want variable" is like this:

1. if first.person then want = 1; if year = lag(year) and lag(want) = 1 then want = 1;

2. When the want variable =1, I would like to save the company as a new "save variable". (Is it possible to save multiple variables into the save variable?)

3. If the company of current observation equals to the save variable, then want = 0; else if there is an observation which has the same company with the save variable in the same year, then want = 0; else want = 1;

4. And everytime the want variable changes from 0 to 1, I would like to update the save variable to the company with the corresponding value of 1.

 

If you know an easy way to solve this problem, please let me know.

 

Thanks for your help.

Patrick
Opal | Level 21

@cool1993 

I believe the bit you will be able to do for us:

Provide a fully working SAS data step posted via the running man icon above which creates the sample data and also includes at least your first condition: if first.person then want = 1;

 

Why is your Want=1 for below case. Which one of your rules applies here?

B 2010 dd 1

 

I would like to save the company as a new "save variable".

Then please add also a new column save_want to your table with the desired result so we can see what you expect to get.

cool1993
Fluorite | Level 6
I added the detailed info. and B 2010 dd has the value of 1 by the rule 3.

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
  • 4 replies
  • 1153 views
  • 0 likes
  • 3 in conversation