简体   繁体   English

通过SAS中的函数运行所有变量

[英]Running All Variables Through a Function in SAS

I am new to SAS and need to sgplot 112 variables. 我是SAS新手,需要sgplot 112个变量。 The variable names are all very different and may change over time. 变量名称都非常不同,可能会随时间而变化。 How can I call each variable in the statement without having to list all of them? 如何在语句中调用每个变量而不必列出所有变量?

Here is what I have done so far: 这是我到目前为止所做的:

%macro graph(var);
proc sgplot data=monthly;
series x=date y=var;
title 'var';
run;
%mend;

%graph(gdp);
%graph(lbr);

The above code can be a pain since I have to list 112 %graph() lines and then change the names in the future as the variable names change. 上面的代码可能很痛苦,因为我必须列出112%graph()行,然后在变量名称更改时更改名称。

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

List processing is the concept you need to deal with something like this. 列表处理是处理这类事务所需的概念。 You can also use BY group processing or in the case of graphing Paneling in some cases to approach this issue. 您还可以使用BY组处理,或者在某些情况下使用图形化面板来处理此问题。

Create a dataset from a source convenient to you that contains the list of variables. 从包含变量列表的方便的源创建数据集。 This could be an excel or text file, or it could be created from your data if there's a way to programmatically tell which variables you need. 这可以是excel或文本文件,或者如果有一种方法可以通过编程方式告知您需要哪些变量,则可以从您的数据创建它。

Then you can use any of a number of methods to produce this: 然后你可以使用任何一种方法来产生这个:

proc sql;
  select cats('%graph(',var,')') 
    into: graphlist separated by ' '
    from yourdata;
quit;

&graphlist

For example. 例如。

In your case, you could also generate a vertical dataset with one row per variable, which might be easier to determine which variables are correct: 在您的情况下,您还可以生成每个变量一行的垂直数据集,这可能更容易确定哪些变量是正确的:

data citiwk;
  set sashelp.citiwk;
  var='COM';
  val=WSPCA;
  output;
  var='UTI';
  val=WSPUA;
  output;
  var='INDU';
  val=WSPIA;
  output;
  val=WSPGLT;
  var='GOV';
  output;
  keep val var date;
run;
proc sort data=citiwk;
  by var date;
run;

proc sgplot data=citiwk;
  by var;
  series x=date y=val;   
run;

While I hardcoded those four, you could easily create an array and use VNAME() to get the variable name or VLABEL() to get the variable label of each array element. 当我硬编码这四个时,您可以轻松创建一个数组并使用VNAME()获取变量名称或VLABEL()来获取每个数组元素的变量标签。

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

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