if index_year=1990 then &name.0=&name.1990;
Hi all,
I am a relatively new SAS user and currently working with longitudinal data. In order to shape my data I am running a number of repetitive commands daily. For example, I need to assign time point depending on the index year. If index year is 1990, then timepoint starts from 1990. I have many variables and 28 time points and this makes my codes are just so bulky. Do you have any suggestions how I can apply macros or loops to save space and writing time? Thank you.
%let name=_alcdep;
DATA db;
set db;
if index_year=1990 then &name.1=&name.1991;
if index_year=1990 then &name.2=&name.1992;
if index_year=1990 then &name.3=&name.1993;
if index_year=1990 then &name.4=&name.1994;
if index_year=1990 then &name.5=&name.1995;
...
if index_year=1991 then &name.0=&name.1991;
if index_year=1991 then &name.1=&name.1992;
if index_year=1991 then &name.2=&name.1993;
if index_year=1991 then &name.3=&name.1994;
if index_year=1991 then &name.4=&name.1995;
....
run;
Using arrays?
array names {0:5} &name.0-&name.5;
array oldnames {0:5} &name.1990-&name.1995;
if index_year=1990 then do i = 1 to 5;
names{i} = oldnames{i};
end;
and I guess you could expand that logic even for your index_year values.
Edit: deleted a mis-typed character;
Making long tables will end you up in awkward programming situations, so avoid it whenever possible.
But for your case, wrap your code into a macro, and use a %do loop to assign the year to your variables dynamically.
Using arrays?
array names {0:5} &name.0-&name.5;
array oldnames {0:5} &name.1990-&name.1995;
if index_year=1990 then do i = 1 to 5;
names{i} = oldnames{i};
end;
and I guess you could expand that logic even for your index_year values.
Edit: deleted a mis-typed character;
Thank you so much! this is exactly what I was looking for.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: