If your variables are all sequentially ordered, use an array to create your variables automatically using the shortcut:
var<min> - var<max>
For example, var1-var4 is automatically interpreted as var1 var2 var3 var4.
Use the new variable's actual name to compare against the year. The below code creates a binary 1/0 variable prefixed with f for each year.
Simple Answer
data want;
set have;
array f[*] f2017-f2020;
/* Grab the year part of the variable name and compare it to the year. */
do i = 1 to dim(f);
f[i] = ( input(substr(vname(f[i]), 2), 4.) = year );
end;
drop i;
run;
Output:
year f2017 f2018 f2019 f2020
2017 1 0 0 0
2018 0 1 0 0
2019 0 0 1 0
2020 0 0 0 1
More Dynamic Version
If you need this to be a little more dynamic, you can read in the min/max year from a macro variable. For example:
proc sql noprint;
select min(year)
, max(year)
into :min_year
, :max_year
from have
;
quit;
data want;
set have;
array f[*] f&min_year. - f&max_year.;
...
Even More Dynamic Version
And if you really needed to get dynamic, you can read in every single value into a space-separated macro variable list. A single macro variable will max out at 65,534 characters, so you have plenty of room to add variables.
proc sql noprint;
select distinct cats('f', year)
into :values separated by ' '
from have
;
quit;
data want;
set have;
array f[*] &values.;
...