简体   繁体   English

R中的SAS宏变量

[英]SAS Macro variable in R

I am very familiar with the SAS programing environment. 我对SAS编程环境非常熟悉。 I am currently trying to learn to program in R. I have found that using SAS Macros reduces the amount of repetitive code in my programming. 我目前正在尝试学习使用R编程。我发现使用SAS宏可以减少编程中重复代码的数量。 Particularly, I have found useful adjusting parts of datasets names and variable names using macro variables. 特别是,我发现使用宏变量有用的调整数据集名称和变量名称的部分。 However, in RI haven't found something that can replicate this. 然而,在RI中没有找到可以复制这个的东西。

For example, in SAS I could write a simple macro to run proc means on two datasets like this: 例如,在SAS中,我可以编写一个简单的宏来在两个数据集上运行proc,如下所示:

%macro means(dataset_suffix = , var1_suffix= );
proc means data = data&dataset_suffix;
var var1&var1_suffix;
run;
%mend means;
%means(dataset_suffix = _suf1, var1_suffix = _suf2);
%means(dataset_suffix = _suf3, var1_suffix = _suf4);

running this code executes the macro two times resulting in the following code being run 运行此代码会执行两次宏,从而导致运行以下代码

proc means data = data_suf1;
var var_suf2;
run;
proc means data = data_suf3;
var var_suf4;
run;

I have looked into R's user defined functions as well as using lists. 我已经研究了R的用户定义函数以及使用列表。 I know there isn't a procedure in R that is directly comparable to proc means. 我知道R中没有一个与proc手段直接相似的程序。 However, this focus of my question is how to use macro variables to reference different objects in R that have similar prefixes but different suffixes. 然而,我的问题的焦点是如何使用宏变量来引用R中具有相似前缀但不同后缀的不同对象。 I have also considered using the paste function. 我也考虑过使用粘贴功能。 Any help with this would be most appreciated. 任何有关这方面的帮助将非常感激。

It always takes some adjustment coming from a macro-heavy language (SAS or Stata) to one that has real variables (R). 它总是需要一些调整,从宏观重型语言(SAS或Stata)到具有实变量(R)的语言。 In the end, you'll find that real variables are more powerful and less error-prone. 最后,您会发现真正的变量更强大,更不容易出错。

Just about everything in R is a first-class object. 几乎R中的所有东西都是一流的对象。 And a list can store just about any object. list可以存储任何对象。 That means you can have lists of model objects, data.frames , whatever you want. 这意味着您可以拥有模型对象列表, data.frames ,无论您想要什么。

datasets <- list( one=data.frame(x=runif(100),y=runif(100) ), two=data.frame(x=runif(100),y=runif(100) ) )
lm(y~x, data=datasets$one)
modelList <- lapply( datasets, function(dat) lm(y~x, data=dat) ) 

Returns a list of model results: 返回模型结果列表:

> modelList
$one

Call:
lm(formula = y ~ x, data = dat)

Coefficients:
(Intercept)            x  
    0.46483      0.06038  


$two

Call:
lm(formula = y ~ x, data = dat)

Coefficients:
(Intercept)            x  
    0.48379      0.00948  

Which you can then operate on: 然后你可以操作:

sapply(modelList,coef)
                   one         two
(Intercept) 0.46482610 0.483785135
x           0.06038169 0.009480099

Starting to see the power yet? 开始看电力了吗? :-) :-)

You could do the same thing with loops, but *apply commands save you a lot of book-keeping code. 您可以使用循环执行相同的操作,但*apply命令可以为您节省大量的簿记代码。

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

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