循环遍历所有变量 [英] Cycling through all variables

查看:48
本文介绍了循环遍历所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在这里学习 SAS,并且基础知识已经掌握得很好,但对我目前知识领域之外的一些问题有疑问.有没有人碰巧知道循环遍历 SAS 数据集中所有变量的方法?我知道如何对范围 (x1-x99) 中的变量运行 do 循环/数组,但理想情况下希望查看每个变量而无需重命名任何变量.基本上,我希望在当前值 = 'True'/'False' 时遍历数据集并更改变量值.我的猜测是我需要在这里以某种方式使用 proc 内容,但不确定如何正确使用它.任何提示/见解将不胜感激.谢谢!

I started to learn SAS here fairly recently and am getting the basics down pretty well, but have a question regarding something that is a little outside of my current realm of knowledge. Does anyone happen to know of a way to cycle through all variables in a SAS dataset? I know how to run a do loop/array on variables in a range (x1-x99), but ideally would like to look at every variable without having to rename any variables. Basically, I'm looking to run through a dataset and change variable values when the current value = 'True'/'False'. My guess is that I'll need to use proc contents in someway here, but not really sure how to go about using it correctly. Any tips/insight would be greatly appreciated. Thanks!

推荐答案

您可以创建一组名称不同的变量.您使用 PROC CONTENTS 走在正确的轨道上,尽管您也可以使用 dictionary.columnssashelp.vcolumn,它们包含基本相同的内容信息.

You can create an array of non-similarly-named variables. You're on the right track with PROC CONTENTS, although you also can use dictionary.columns or sashelp.vcolumn, which contain basically the same information.

proc sql;
select name into :collist separated by ' ' 
from dictionary.columns
where memname='DATASETNAME' and libname='LIBNAME' and <other criteria>;
quit;

变量必须是相同类型(字符/数字),因此您可能希望在查询中包含变量类型标准,以及您可能需要的任何其他限制因素.

The variables have to be all of the same type (char/numeric) so you may want to include a criterion of variable type in your query, plus any other limiting factor you may need.

这将在一个宏变量中创建一个列表 &collist.,您可以在数组中使用

That will create a list, &collist., in a macro variable you can use in your array

array vars &collist.;

现在你可以遍历数组了.

and now you can loop over the array.

如果你的所有变量都是相同的类型,并且你知道顺序是固定的,你也可以作弊.双破折号列表 (x1--x99) 是按变量顺序排列的,所有变量从 x1 到 x99",不需要数字后缀或类似的东西.

You may also be able to cheat things, if all of your variables are the same type, and you know the order is fixed . The double dash list (x1--x99) is 'in variable order, all variables from x1 to x99' and doesn't require numeric suffixes or anything like that.

最后,您还可以在 PROC FORMAT 中编写格式来完成您需要的操作,具体取决于您打算做什么(将 TRUE 映射到 1,将 FALSE 映射到 0 或类似那个).

Finally, you also might be able to write a format in PROC FORMAT to accomplish what you need, depending on what you are intending to do (mapping TRUE to 1 and FALSE to 0 or something like that).

这篇关于循环遍历所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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