R如何编写SAS这样的宏
我有这样一个很长的sqlquery,只需更改日期就可以运行几次。我擅长SAS,但对R来说是个新手,所以我很难写出类似SAS的东西R如何编写SAS这样的宏,r,macros,rodbc,R,Macros,Rodbc,我有这样一个很长的sqlquery,只需更改日期就可以运行几次。我擅长SAS,但对R来说是个新手,所以我很难写出类似SAS的东西 df <- sqlQuery(datamart, paste0("Select xxxxxxxxxxxxxxxxxx from xxxxx where date = '28Feb2018'"), as.is=TRUE, stringsAsFactors = FALSE) df有几种方法可以做到这一点,但既然你说你是R新手,这可能是一种自然的方法。首先,创建一个
df <- sqlQuery(datamart, paste0("Select xxxxxxxxxxxxxxxxxx from xxxxx
where date = '28Feb2018'"), as.is=TRUE, stringsAsFactors = FALSE)
df有几种方法可以做到这一点,但既然你说你是R
新手,这可能是一种自然的方法。首先,创建一个函数,该函数允许您更改选择_cols
、tbl
和date
,并返回一个字符串:
make_query <- function(select_cols, tbl, date) {
paste0("SELECT ", select_cols, " FROM ", tbl, " WHERE date = '", date, "';")
}
make_query("*", "my_table", "28Feb2018")
[1] "SELECT * FROM my_table WHERE date = '28Feb2018';"
make_query("*", "different_table", "28Feb2018")
[1] "SELECT * FROM different_table WHERE date = '28Feb2018';"
而且,由于看起来您希望保存结果,您可以预先分配一个与日期数相同长度的空列表,然后保存这些结果:
df <- vector("list", length(various_dates))
for (date in seq_along(various_dates)) {
df[[date]] <- sqlQuery(datamart, make_query("*", "my_table", various_dates[date]),
as.is = TRUE, stringsAsFactors = FALSE)
}
df相当于SAS宏的R是一个函数。因此,您可以编写一个函数,将日期作为参数,然后将日期传递给查询
最简单的方法是通过字符串操作:
qry <- function(date_string)
{
qry_string <- paste0("select xxxxx from yyy where date = '", date_string, "'")
sqlQuery(datamart, qry_string, as.is=TRUE, stringsAsFactors=FALSE)
}
qry最简单的方法如下所示:
macro1 <- function(dt) {
qry <- paste0("select xxxxxxxxxx from xxxx where date='", dt, "'")
sqlQuery(datamart, qry, as.is=TRUE, stringsAsFactors=FALSE)
}
虽然更好的解决方案是使用变量绑定(“参数化查询”),这对于每种数据库类型都是唯一的。正如Hong Ooi所建议的,rodbext
为RODBC
连接提供了这一功能,但在其他方面,您将需要更特定于数据库的功能
如果您想稍微懒一点,并且觉得连接总是在全局中是安全的,那么您可能会尝试执行以下操作:
macro2 <- function(dt, con) {
if (length(dt) == 0L) {
stop("'dt' is not length 1")
} else if (length(dt) > 1L) {
warning("'dt' has length > 1 and only the first element will be used")
dt <- dt[[1L]]
}
qry <- sprintf("select xxxxxxxxxx from xxxx where date='%s'", sQuote(dt))
sqlQuery(con, qry, as.is=TRUE, stringsAsFactors=FALSE)
}
macro2 <- function(dt, con=datamart) ...
answers <- lapply(vector_of_dates, macro2)
可以使用for循环for(date in dates){…}
,然后使用paste0
将date
粘贴到您的SQL中。C_Mu,其中一个答案解决了您的问题吗?C_Mu,您忽略这些答案有什么原因吗?如果他们不能解决你的问题,我建议你说明为什么他们不能(足够)工作。如果其中至少有一个是足够的,请。我终于想出了如何做到这一点,这些答案给了我很多提示,这里是最终的代码作品。谢谢大家在这里帮助我。query_string=函数(表,日期列,日期){x=paste0('Delete From',表,'Where',日期列,sqlQuery
的第一个参数似乎是连接对象或类似对象,而不是查询字符串。这是我首选的方法。除了规避SQL注入攻击之外,sqlExecute
还将为您管理重复。
safeqry <- function(date_string)
{
require(RODBCext)
qry_string <- paste0("select xxxxx from yyy where date = ?")
parms <- data.frame(date_string, stringsAsFactors=FALSE)
sqlExecute(datamart, qry_string, parms, fetch=TRUE)
}
macro1 <- function(dt) {
qry <- paste0("select xxxxxxxxxx from xxxx where date='", dt, "'")
sqlQuery(datamart, qry, as.is=TRUE, stringsAsFactors=FALSE)
}
macro2 <- function(dt, con) {
if (length(dt) == 0L) {
stop("'dt' is not length 1")
} else if (length(dt) > 1L) {
warning("'dt' has length > 1 and only the first element will be used")
dt <- dt[[1L]]
}
qry <- sprintf("select xxxxxxxxxx from xxxx where date='%s'", sQuote(dt))
sqlQuery(con, qry, as.is=TRUE, stringsAsFactors=FALSE)
}
macro2 <- function(dt, con=datamart) ...
answers <- lapply(vector_of_dates, macro2)