如何读取SAS数据集中的变量名?

发布于 2024-10-24 10:12:01 字数 384 浏览 4 评论 0原文

有没有能够获取变量名称的语句/函数? 最好将它们放入另一个数据集的列、文本字段或宏变量中。

例如

- 数据集 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

已下线请稍等 2024-10-31 10:12:01

PROC CONTENTS 将是在数据集中获取该信息的最快方法。列名称可以在列 NAME 中找到。

proc contents data=sashelp.class out=contents noprint;
run;

PROC CONTENTS would be the quickest way to get that information in a dataset. Column names can be found in the column NAME.

proc contents data=sashelp.class out=contents noprint;
run;
晨曦÷微暖 2024-10-31 10:12:01

您还可以使用数据步和数组函数,例如

data colnames ;
  set sashelp.class (obs=1) ;

  array n{*} _NUMERIC_ ;
  array c{*} _CHARACTER_ ;

  do i = 1 to dim(n) ;
    vname = vname(n{i}) ;
    output ;
  end ;
  do i = 1 to dim(c) ;
    vname = vname(c{i}) ;
    output ;
  end ;
run ;

You can also use a datastep and array functions, e.g.

data colnames ;
  set sashelp.class (obs=1) ;

  array n{*} _NUMERIC_ ;
  array c{*} _CHARACTER_ ;

  do i = 1 to dim(n) ;
    vname = vname(n{i}) ;
    output ;
  end ;
  do i = 1 to dim(c) ;
    vname = vname(c{i}) ;
    output ;
  end ;
run ;
惟欲睡 2024-10-31 10:12:01
%macro getvars(dsn);
 %global vlist;
 proc sql;
 select name into :vlist separated by ' '
  from dictionary.columns
  where memname=upcase("&dsn");
 quit;
%mend;

这将创建一个名为 &vlist 的宏变量,它将包含数据集中所有变量的名称,并以空格分隔。如果您想在变量名称之间使用逗号,只需将“分隔符”值从“ ”更改为“, ”即可。在 where 语句中使用 upcase 函数可以避免有人以错误的大小写传递数据集名称的问题。需要全局语句,因为如果不将其定义为全局,则创建的宏变量不一定在宏外部可用

%macro getvars(dsn);
 %global vlist;
 proc sql;
 select name into :vlist separated by ' '
  from dictionary.columns
  where memname=upcase("&dsn");
 quit;
%mend;

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

仲春光 2024-10-31 10:12:01

与 SAS 帮助和文档略有不同。

%macro names(dsid);
  %let dsid=%sysfunc(open(&dsid, i));
  %let num=%sysfunc(attrn(&dsid,nvars));
  %let varlist=;
  %do i=1 %to &num  ;
    %let varlist=&varlist %sysfunc(varname(&dsid, &i));
  %end;
  %let rc = %sysfunc(close(&dsid)); /*edit by Moody_Mudskipper: omitting this line will lock the dataset */
  %put varlist=&varlist;
%mend names;

%names(sasuser.class) ;

然后,即使数字和字符混合,我们也会保留数据的大小写和顺序。

Slightly changed from SAS help and documentation.

%macro names(dsid);
  %let dsid=%sysfunc(open(&dsid, i));
  %let num=%sysfunc(attrn(&dsid,nvars));
  %let varlist=;
  %do i=1 %to &num  ;
    %let varlist=&varlist %sysfunc(varname(&dsid, &i));
  %end;
  %let rc = %sysfunc(close(&dsid)); /*edit by Moody_Mudskipper: omitting this line will lock the dataset */
  %put varlist=&varlist;
%mend names;

%names(sasuser.class) ;

Then we preserve case and the order off data, even if numeric and character is mixed.

酒解孤独 2024-10-31 10:12:01

我不确定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文