Hello, 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? Thanks for any suggestions -- View this message in context: http://r.789695.n4.nabble.com/Paasing-values-to-sqlQuery-like-SAS-macro-tp4643033.html Sent from the R help mailing list archive at Nabble.com.
Paasing values to sqlQuery like SAS macro
3 messages · ramoss, Thomas Lumley
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
Thanks I was doing something similar in SAS. I was looping macro based on a
dataset containing the values:
data _null_;
set summary2;
mindat=put(datepart(mindate),date9.);
min_date='mindat_'|| trim(left(_n_));
put mindate= mindat= min_date=; /*check values in log*/
call symput (min_date,compress("'"||mindat||"'"));
maxdat=put(datepart(maxdate),date9.);
max_date='maxdat_'|| trim(left(_n_));
call symput (max_date,compress("'"||maxdat||"'"));
var_name='firm_' || trim(left(_n_));
put var_name= firm=;
call symput (var_name,compress("'"||firm||"'"));
call symput ('total_obs',_n_);
run;
/* To test macro vars*/
%macro affiliated;
%do i=1 %to &total_obs;
%put firm_i=&&firm_&i;
%put mindat=&&mindat_&i;
%put maxdat=&&maxdat_&i;
%end;
%mend;
%affiliated;
/* END*/
So you are saying in R I can use the functions that you mention to subtract
the info from dataframe containing all the values & pass it on? I am using
the sqldf package.
I am using sqldf package.
--
View this message in context: http://r.789695.n4.nabble.com/Paasing-values-to-sqlQuery-like-SAS-macro-tp4643033p4643078.html
Sent from the R help mailing list archive at Nabble.com.