简体   繁体   中英

Running All Variables Through a Function in SAS

I am new to SAS and need to sgplot 112 variables. 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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