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.