简体   繁体   English

SAS Loop重命名变量

[英]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. 基本上,我有许多变量称为f2 f3 f4,依此类推,直到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. 逻辑如下:我需要将变量f14 f18的值替换为4,直到f54,再由变量f2 f3,f4到f12的值替换。 f13, f55 f56 and f57 are left as they are. f13,f55,f56和f57保持不变。 Then I need to replace f70 f74 and so on by 4 up to f110 by values of f58 f59 up to f68. 然后,我需要用f58 f59到f68的值将f70 f74等替换为4到f110。 So f69 is unchanged. 因此f69不变。 So I need to say SAS the following: 因此,我需要对SAS进行以下说明:

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). 我该如何自动执行此任务,甚至可能删除我用于替换的变量(我的意思是像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 信息: 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. 您也可以使用宏变量来执行此操作,但是由于宏会增加复杂性,因此除非总有必要,否则最好避免使用宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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