0

I would like to use the concept of macro variable from SAS in r but cannot seem to find the right code. It would normally look like this in SAS where I change the name once and it replicates many places.

%LET CITY=NewYork; 
Query_&NAME_V01 <- &NAME[,1:3]
Query_&NAME_V01 <- &NAME[1:10,]

I tried using the quote and noquote function but I always end up with the this error message: target of assignment expands to non-language object.

stuski
  • 199
  • 1
  • 11
  • 1
    You need to edit to make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610). – alistaire Jul 06 '17 at 14:47
  • Is what you want to do possible? Yes. But it's a bad way to go about the problem. Try explaining the goal you want to achieve and we can help you do it in a better way. – Dason Jul 06 '17 at 14:47
  • I essentially want to speed up the work by no changing manually those variable for ad-hoc analysis. – stuski Jul 06 '17 at 14:59

1 Answers1

3

I recently had to learn some SAS and trust me I cannot think to something like SAS macros in R. At least not something that it is as easy to use. But trust me, R is much more powerful, you have just to think like R (and most of programming languages nowadays) and not like SAS.

Put your data in a list and use a for loop here an example that print the colnames of dataframe(cat is just the print function)

example<-list(airquality,iris,mtcars)
for(i in 1:length(example)){
  cat(colnames(example[[i]]),"\n")
}

You could use non standard evaluation to imitate SAS behaviour, here a reference http://adv-r.had.co.nz/Computing-on-the-language.html but trust me It is something that it does not fit to a newbie. Even R experienced users struggle with this.

Federico Manigrasso
  • 1,130
  • 1
  • 7
  • 11