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
I think this is all you need.
if start <= start_year <= finish then flag=1; else flag=0;
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?
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
I think this is all you need.
if start <= start_year <= finish then flag=1; else flag=0;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.