如何读取SAS数据集中的变量名?
有没有能够获取变量名称的语句/函数? 最好将它们放入另一个数据集的列、文本字段或宏变量中。
例如
- 数据集 1
Name age sex
Jk 14 F
FH 34 M
预期数据集
数据集的变量名称
<前><代码>名称 年龄 性别
PS:我知道一个语句:select into,它会相关地做某事 它可以将列的值读入具有自定义分隔符的字段中,因此希望有类似的方法将列名读入字段或列中。
谢谢
Are there any statements\functions capable of get the name of variables?
Preferrably putting them into a column of another data set, a text field or a macro variable.
E.g.
- Data set 1
Name age sex
Jk 14 F
FH 34 M
Expected data set
Var_name_of_dataset1
Name age sex
PS: I know a statement: select into, which does sth relevantly
It can read the value of a column into a field with customized separetors, and therefore wish there are similar ways of reading column names into a field or a column.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PROC CONTENTS 将是在数据集中获取该信息的最快方法。列名称可以在列 NAME 中找到。
PROC CONTENTS would be the quickest way to get that information in a dataset. Column names can be found in the column NAME.
您还可以使用数据步和数组函数,例如
You can also use a datastep and array functions, e.g.
这将创建一个名为 &vlist 的宏变量,它将包含数据集中所有变量的名称,并以空格分隔。如果您想在变量名称之间使用逗号,只需将“分隔符”值从“ ”更改为“, ”即可。在 where 语句中使用 upcase 函数可以避免有人以错误的大小写传递数据集名称的问题。需要全局语句,因为如果不将其定义为全局,则创建的宏变量不一定在宏外部可用
This creates a macro variable called &vlist that will contain the names of all the variables in your dataset, separated by a space. If you want commas between the variable names, all you have to do is change the 'separated by' value from ' ' to ', '. The use of the upcase function in the where statement avoids problems with someone passing the dataset name in the wrong case. The global statement is needed since the macro variable created will not necessarily be available outside the macro without defining it as global
与 SAS 帮助和文档略有不同。
然后,即使数字和字符混合,我们也会保留数据的大小写和顺序。
Slightly changed from SAS help and documentation.
Then we preserve case and the order off data, even if numeric and character is mixed.
我不确定 Rawfocus 断言读取字典表查询所有库是否正确,如果使用 sashelp.vcolumn 示例,那么它会是正确的,该方法非常慢并且确实访问分配的所有库。 (您可以使用 SAS RTRACE 系统选项来证明这一点。)
我认为对dictionary.columns 进行sql 查询是此处概述的方法中最快的。显然,宏化代码可以在没有宏的情况下工作,但我认为这里宏的要点是作为一个实用程序;将代码放入您最喜欢的宏库中,您再也不需要考虑它了。
I'm not sure Rawfocus assertion that reading dictionary tables queries all libraries is true, had the example used sashelp.vcolumn instead then it would be true, that approach is very slow and does access all the libraries allocated. (You can prove this with the SAS RTRACE system option.)
I am of the opinion that a sql query to dictionary.columns is the fastest of the methods outlined here. Obviously the macrotised code would work without the macro but the point of the macro here is I think as a utility; put the code into your favourite macro library and you never need to think about it again.