BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
msxas90
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

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.

Data never sleeps
Kurt_Bremser
Super User

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;

msxas90
Fluorite | Level 6

Thank you so much! this is exactly what I was looking for. 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1319 views
  • 2 likes
  • 3 in conversation