简体   繁体   中英

SAS Loop for renaming variables

I want to replace values of some variables and rename them. Basically, I have many variables called f2 f3 f4 and so on up to f1065. The logic is as folllows: I need to replace values of variables f14 f18 and so on by 4 up to f54 by values of variables f2 f3 f4 up to f12. f13, f55 f56 and f57 are left as they are. Then I need to replace f70 f74 and so on by 4 up to f110 by values of f58 f59 up to f68. So f69 is unchanged. So I need to say SAS the following:

let k=2
do i=4 to 57 by 4;
f&i=f&k;
end;
k=&k+1;

and do this many times, so that the next time it will be

let k=58
do i=70 to 113 by 4;
f&i=f&k;
end;
k=&k+1;

How can I automise this task and possibly even delete the variables that I use for replacement (i mean variables like f2 f3 f4 ... f12). Thanks in advance!

You can accomplish this with arrays. Simply define an array for a section of your variables, and use an index to iterate through them. Info: http://www2.sas.com/proceedings/sugi30/242-30.pdf

You could also do it with macro variables, but since macros add complexity, it's almost always better to avoid unless you really need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM