Let me introduce an alternative solution, because there is no need to go in a conceptual workaround through character strings that need to be analyzed:
R allows you to manipulate expressions at run time (without encoding them as strings, that is), using a set of functions such as substitute or bquote . bquote is probably closest to the SAS approach:
depvar = quote(sales.int) reg = lm(bquote(.(depvar) ~ var1 + var2), mydata)
bquote essentially takes an R expression and replaces each variable that is surrounded by a value .(β¦) by its value. The first line gives the name of the variable - this is very similar to the actual macro in SAS. This should be surrounded by quote , because otherwise R will try to assign the contents of sales.int depvar and not its name. quote works identically to bquote , except that you cannot use the syntax .(β¦) in it to replace existing variables.
You can also determine this name from user input (i.e., from a character string using as.name :
depvar = as.name('sales.int')
as.name converts a character string to an object name R.
Just a comment on language design, as this is a common misunderstanding: R may be less intuitive than SAS in this regard, but it is conceptually much more consistent and generally applicable. Statistical packages other than R essentially provide hacks for working on the language, while R integrates formulas perfectly into the language.
source share