In a previous query, I wanted to find out if I could find a solution to repetitive process similar to SAS macro variables. The link is as follows:
R macros to enable user defined input similar to %let in SAS
However I was looking to take this a step forward by exploring the potential of specifying a character list instead of user inputting the values for the macro variable (calling it macro variable for convenience) every time.
For example, here is a short extract of the code i am working on which specifies the use of the macro variables using the paste0
function:
### Change metric between MSP, RSP, Val, Price MSP, Price RSP, Margin
### Change level between product and channel
### Change histyr, baseyr, futeyr according to year value
metric <- "RSP"
level <- "channel"
histyr <- "2009"
baseyr <- "2014"
futeyr <- "2019"
macro <- "gni"
macro1 <- "pce"
inputpath <- "C:/Projects/Consumption curves/UKPOV/excel files/"
outpath <- "C:/Projects/Consumption curves/UKPOV/output/gen_output/"
infile <- paste0(inputpath, metric, "_", level, "_CC_", histyr, "_salespercap.csv")
Category_sales <- read.csv(infile)
macroeco_data <- read.csv(infile2)
macroeco_data$Country <- str_trim(macroeco_data$Country)
sales_nd_macroeco <- sqldf("SELECT L.*, R.gnippp_histyr as GNI_PPP, R.SR_histyr as SR
FROM Category_sales L LEFT JOIN macroeco_data R
ON (L.Country = R.Country) order by GNI_PPP DESC")
Now instead of specifying each metric every time I was looking to create a character list or a character vector and use loops to run for every metric without manual intervention.
I tried the following but it doesnt seem to work. Not sure if I am doing this right
metric <- c("MSP", "RSP", "Vol", "PriceMSP", "PriceRSP", "Margin")
for (i in metric) {
level <- "channel"
histyr <- "2009"
baseyr <- "2014"
futeyr <- "2019"
macro <- "gni"
macro1 <- "pce"
inputpath <- "C:/Projects/Consumption curves/UKPOV/excel files/"
outpath <- "C:/Projects/Consumption curves/UKPOV/output/gen_output/"
infile <- paste0(inputpath, metric[i], "_", level, "_CC_", histyr, "_salespercap.csv")
Category_sales <- read.csv(infile)
infile2 <- paste0(inputpath,"macro_",histyr,".csv")
macroeco_data<- read.csv(infile2)
macroeco_data$Country<-str_trim(macroeco_data$Country)
sales_nd_macroeco <- sqldf("SELECT L.*, R.gnippp_histyr as GNI_PPP, R.SR_histyr as SR
FROM Category_sales L LEFT JOIN macroeco_data R
ON (L.Country = R.Country) order by GNI_PPP DESC")
}
The error is as below:
metric<-c("MSP","RSP", "Vol","PriceMSP" ,"PriceRSP", "Margin")
> metric
[1] "MSP" "RSP" "Vol" "PriceMSP" "PriceRSP" "Margin"
> for(i in metric){
+ level<-"channel"
+ histyr<-"2009"
+ baseyr<-"2014"
+ futeyr<-"2019"
+ macro<-"gni"
+ macro1<-"pce"
+ inputpath<-"C:/Projects/Consumption curves/UKPOV/excel files/"
+ outpath<-"C:/Projects/Consumption curves/UKPOV/output/gen_output/"
+ infile <- paste0(inputpath,metric[i],"_",level,"_CC_",histyr,"_salespercap.csv")
+ Category_sales <- read.csv(infile)
+ infile2 <- paste0(inputpath,"macro_",histyr,".csv")
+ macroeco_data<- read.csv(infile2)
+ macroeco_data$Country<-str_trim(macroeco_data$Country)
+ sales_nd_macroeco <- sqldf("SELECT L.*, R.gnippp_histyr as GNI_PPP, R.SR_histyr as SR
+ FROM Category_sales L LEFT JOIN macroeco_data R
+ ON (L.Country = R.Country) order by GNI_PPP DESC")
+ }
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:/Projects/Consumption curves/UKPOV/excel files/NA_channel_CC_2009_salespercap.csv': No such file or directory
>