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. 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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