[R] Paasing values to sqlQuery like SAS macro

Thomas Lumley tlumley at uw.edu
Thu Sep 13 23:07:38 CEST 2012


On Fri, Sep 14, 2012 at 3:54 AM, ramoss <ramine.mossadegh at finra.org> wrote:

> We lost our SAS licence & I am busy transfering my old SAS programs to R
> environment.  I am very new to R.  In 1 program I was creating SAS macro
> vars & passing them into a SQL query to run against the server. There are 3
> variables firm, begindt, enddt.  # of values for each varies month to month.
> Is there anyway I could do the same thing in R & pass the afore mentioned
> values to a query running within sqlQuery?

Almost certainly, but you may have to provide more detail about what
you want (and it's a good idea to mention what package sqlQuery is in,
when asking a question like this)

One approach, based on a guess about what you want, is to take each
value in turn and use gsub() or paste() to put them into a query
string.  For example

queries<-paste("select x, y, z, firm, date from the_appropriate_table
where firm=", firms," and date>=", begindts, " and date <=", enddts)
lapply(queries, sqlQuery, channel=my.database.connection)

will return a list of data frames, one for each set of values.

   -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland




More information about the R-help mailing list