Create a flag to determine if year is within desired time period

Solved
Occasional Contributor
Posts: 9

Create a flag to determine if year is within desired time period

[ Edited ]

Hello Dear SAS users,

I have the following tricky problem and I would appreciate your great help:

I have data, where I know the start and the end (finish) year per id as you see below. I want to assign 0 to a record that is outside of the range and 1 otherwise:

id    start     finish         start_year         end_year        want

1     2010    2015         2008                 .                       0

1     2010    2015         2009                 .                       0

1     2010    2015         2010                2011                 1

1     2010    2015         2011                2012                 1

1     2010    2015         2012                2013                 1

1     2010    2015         2013                2014                 1

1     2010    2015         2014                2015                 1

1     2010    2015         2015                .                        1

1     2010    2015         2016                .                        0

2     2008    2016         2008                2009                 1

2     2008    2016         2009                 .                       1

2     2008    2016         2010                2010                 1

2     2008    2016         2011                2012                  1

2     2008    2016         2012                .                        1

2     2008    2016         2013                2013                 1

2     2008    2016         2014                2015                 1

2     2008    2016         2015                2016                 1

2     2008    2016         2016                .                        1

3     2013    2014         2008                .                        0

3     2013    2014         2009                 .                       0

3     2013    2014         2010                .                        0

3     2013    2014         2011                .                        0

3     2013    2014         2012                .                        0

3     2013    2014         2013                2014                 1

3     2013    2014         2014                .                        1

3     2013    2014         2015                .                        0

3     2013    2014         2016                .                        0

Accepted Solutions
Solution
‎01-13-2017 06:33 PM
Super User
Posts: 24,012

Re: assigning ones and zeros up and/or down missing values

I think this is all you need.

``if start <= start_year <= finish then flag=1; else flag=0;``

All Replies
Super User
Posts: 13,941

Re: assigning ones and zeros up and/or down missing values

[ Edited ]

How do we know what is " outside of the range "?

Use the actual column headings you display because "end" or "finish" could be either the variable Finish or End_year. Plus you have have START and START_YEAR.

So the range could be Start-Finish or Start_year-Finish or Start-End_year or Start_year-End_year. And which explicit variable are we considering within which pair?

Occasional Contributor
Posts: 9

Re: assigning ones and zeros up and/or down missing values

[ Edited ]

outside of range would be below the start and above the finish per id as the 0's and 1's illustrate above. So, 0 means outside and 1 means inside the range

Occasional Contributor
Posts: 9

Re: assigning ones and zeros up and/or down missing values

The range I am interested is Start-Finish. Equivalently, we could use Start_year and End_year, but as you see above there are missing values that fall into the range and others that do not. Again, the range is defined by Start and Finish. Thank you and I hope you can provide some guidance with the code.
Solution
‎01-13-2017 06:33 PM
Super User
Posts: 24,012

Re: assigning ones and zeros up and/or down missing values

I think this is all you need.

``if start <= start_year <= finish then flag=1; else flag=0;``
☑ This topic is solved.