在 SAS 中使用循环重命名索引列? [英] In SAS use loop to rename indexed columns?

查看:60
本文介绍了在 SAS 中使用循环重命名索引列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量为 col1、col2、col3、...、col15 的数据集.我想把它们重命名为new1, new2, new3, ..., new 15.我可以写15次类似的rename col1 = new1;在 SAS 中,但如何使用循环实现此目的?谢谢.

I have a dataset with variables as col1, col2, col3, ..., col15. I want to rename them to new1, new2, new3, ..., new 15. I can write 15 times the similar rename col1 = new1; in SAS but how can I achieve this using loop? Thanks.

推荐答案

首先,不清楚你是在谈论 proc datasets 中的 rename 语句还是在数据步骤.如果您不需要对数据执行任何其他操作,则绝对应该使用 proc datasets 来执行此操作,否则(在数据步骤中)您将不必要地读取/写入数据集中的每条记录,只是为了更改变量名称.

Firstly, it's not clear whether you're talking about the rename statement in proc datasets or in the data step. If you don't need to do anything else to the data, you should definitely use proc datasets to do this, because otherwise (in a data step) you're unnecessarily reading/writing every record in the dataset, just to alter variable names.

如果您正在使用数据步骤,只需使用

If you are using the data step, just use

rename col1-col15=new1-new15;

我不确定您是否可以在 proc 数据集中使用该快捷方式.这让我们想到了你的循环问题.除非您多次或动态地执行此操作,否则复制/粘贴代码 15 次可能同样容易.这是一种生成所需语句的方法,将其放入宏变量中,然后在重命名语句中使用该宏变量:

I'm not sure if you can use that shortcut in proc datasets. Which brings us to your looping question. Unless you're doing this lots of times or dynamically, it's probably just as easy to copy/paste the code 15 times. Here's a way to generate the statement you want, put it in a macro variable, and use that macro variable in the rename statement:

data _null_;
  length myVar $ 1000;
  *myVar='';
  do i=1 to 15;
    myVar=catx(' ',myVar,' ',cats('col',i,'=','new',i));
  end;
  call symput('rename',myVar);
run;

%put &rename;

proc datasets library=mylibrary;
  modify mydataset;
  rename &rename;
run;

这篇关于在 SAS 中使用循环重命名索引列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆