more effecient codes for repetitive data steps

Accepted Solution Solved
Reply
Highlighted
New Contributor
Posts: 4
Accepted Solution

more effecient codes for repetitive data steps

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;


Accepted Solutions
Solution
‎03-07-2018 07:44 AM
Super User
Posts: 10,279

Re: more effecient codes for repetitive data steps

[ Edited ]

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 5,884

Re: more effecient codes for repetitive data steps

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
Solution
‎03-07-2018 07:44 AM
Super User
Posts: 10,279

Re: more effecient codes for repetitive data steps

[ Edited ]

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 4

Re: more effecient codes for repetitive data steps

Posted in reply to KurtBremser

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

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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