8

How do I assign a macro variable in R?

In SAS, I would use the following code

%LET DEPVAR = sales_ind

PROC REG DATA=mydata;
MODEL &DEPVAR = VAR1 + VAR2;
RUN;

However, in R, I am struggling to do something similar (this doesn't work)

depvar <<- sales_ind

reg<-lm(depvar ~ var1 + var2, data=mydata)

Any ideas?

Thank you!

Chris L
  • 338
  • 1
  • 4
  • 15

3 Answers3

21

Let me provide an alternative solution, because there’s no need to go the conceptual detour via character strings which have to be parsed:

R allows you to manipulate expressions at runtime (without encoding them as strings, that is), via a set of functions such as substitute or bquote. bquote probably comes closest to the SAS approach:

depvar = quote(sales.int)
reg = lm(bquote(.(depvar) ~ var1 + var2), mydata)

bquote essentially takes an R expression, and replaces every variable which is surrounded by .(…) by its value. The first line assigns a name to a variable – this is very similar to the actual macro in SAS. This needs to be surrounded by quote, because otherwise R would try to assign the contents of sales.int to depvar, rather than its name. quote works identical to bquote, except that you cannot use the .(…) syntax in it to replace existing variables.

You can also determine this name from a user input (i.e. from a character string, by using as.name:

depvar = as.name('sales.int')

as.name converts a character string to an R object name.


Just a comment on the language design, since this a common misunderstanding: R may be less intuitive than SAS in this regard, but it‘s conceptually much more consistent and generally applicable. Non-R statistics packages essentially provide hacks to work around the language while R integrates formulae beautifully into the language itself.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
8

how about this:

reg<-lm(formula(paste(depvar ,'~  var1 + var2')), data=mydata)
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
  • That seems to work! Thank you Jthorpe. It is less intuitive to me than the SAS solution - – Chris L Feb 12 '15 at 17:38
  • 1
    In R you can specify a formula literal (expression) via `a ~ b + c`, or you can create a string with the formula and call `formula()` to create a formula based on the string. (PS. if it the solution works, you can mark it correct to close the question) – Jthorpe Feb 12 '15 at 17:41
  • 1
    I will - it tells me I need to wait 10 minutes before I can mark it correct – Chris L Feb 12 '15 at 17:42
  • 3
    R doesn't have lexer directives which tell the lexer (the part of the program that separates the text into individual tokens) to substitute on bit of text from your code into another bit of text , the way `LET` does in SAS. – Jthorpe Feb 12 '15 at 17:45
-5

None of them works. The macro variable is invoked with & in SAS but no similar & being used in R although you can use new=as.name("f3") or new=quote(f3) to refer to f3 as shown below.

> test<-data.frame(f1=c(1,2), f2=c("a","b")); test
#   f1 f2
#1  1  a
#2  2  b

> new=as.name("f3"); new; 
#f3
> new=quote(f3); new; 
#f3

> # you still get new rather than f3 as expected
> new<-test$f1; 
> new
#[1] 1 2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Andy
  • 1
  • 1