@YD5265 Adding an explanation to why you got the results you did, prior to implementing @PaigeMiller solution.
There are two types of Data Step statements, Declarative & Executable
The first 5 lines (up to and including length CNS_P_LBL $16;) are all declarative, the rest executable.
Executable statements are processed in the order they appear in the data step.
Your first executable statement is
if CNS_P_LBL = "1971-1990" then YEARBUILT = "01/01/1971";
At this point in the data step execution CNS_P_LBL (as is YEARBUILT) is an empty string, it is a known variable as it was declared in the length statement during the compilation phase.
None of your IF statements are true when CNS_P_LBL is an empty string so YEARBUILT will be an empty string. When the execution phase reaches the set INPUT; statement, then CNS_P_LBL is read from the INPUT data set, resulting in its value being set to 1971-1990.
An easy way to see this to understand it, is to use PUT statements within your code (note this is a handy debugging method):
data OUTPUT;
length YEARBUILT $100;
format YEARBUILT $100.;
informat YEARBUILT $100.;
length CNS_P_LBL $16;
PUT "1) " _all_ ; /* _all_ will output all the variable values */
if CNS_P_LBL = "1971-1990" then YEARBUILT = "01/01/1971";
if CNS_P_LBL = "1991-2000" then YEARBUILT = "01/01/1990";
if CNS_P_LBL = "2001-2005" then YEARBUILT = "01/01/2003";
if CNS_P_LBL = "2006-2010" then YEARBUILT = "01/01/2008";
if CNS_P_LBL = "2011-2015" then YEARBUILT = "01/01/2013";
if CNS_P_LBL = "2016-2020" then YEARBUILT = "01/01/2018";
if CNS_P_LBL = "1970 or before" then YEARBUILT = "12/31/9999";
PUT "2) " _all_ ; /* _all_ will output all the variable values */
set INPUT;
PUT "3) " _all_ ; /* _all_ will output all the variable values */
RUN ;
Paige's solution works, because once you move the SET statement prior to the IF statements, then the variable CNS_P_LBL gets assigned prior to the IF statements.
... View more