I am reading in GIS data provided by a soil scientist on my research team that is exported to csv files with numeric names (years only). I have created code to read in a list of csv files from different regions and rename the variables from the v1, v2, v3..... vn format to the format varname_year (here SPI_year) using the variable labels in the name. The problem is that I'd like to set this up to run a loop from the first variable v2 to the last where the last variable name changes depending on the data I'm importing. The loc lastvar: word `c(k)' of `r(varlist)' does not read the last variable since it is not a valid name. Is there a way for Stata to recognize the last variable "vn" in this list? My code is as follows:
***** This does not work. Error: nothing found where name expected
cd "$mypath"
foreach region in region1 region2 {
insheet using "$mypath\SPI_`region'.csv", clear
qui des
loc lastvar: word `c(k)' of `r(varlist)'
foreach v of varlist v2-`lastvar' {
local x : variable label `v'
rename `v' SPI_`x'
}
***** This works but I added the name of the last variable
cd "$mypath"
foreach region in region1 region2 {
insheet using "$mypath\SPI_`region'.csv", clear
foreach v of varlist v2-v469 {
local x : variable label `v'
rename `v' SPI_`x'
}
My data look like this, where lotid is the unique identifier:
lotid v2 v3 v4 v5 v6 v7 v8
13008 1.7 2.34 0.75 0.80 1.23 3.78 0.85
Related Posts with Use the lastvar local to read v1 v2 v3 variables from insheet with numeric names
 error message returned when installing spost13 using Stata 17Hello,
I would like to use Brant test for my ordered logistic regression models and tried to instal… error message returned when installing spost13 using Stata 17Hello,
I would like to use Brant test for my ordered logistic regression models and tried to instal…
 How to rewrite graph-like command that written by users under Stata 6.0 or earlierDear Stata users,
Stata had changed its graph command since version 7. However there are a lot of c… How to rewrite graph-like command that written by users under Stata 6.0 or earlierDear Stata users,
Stata had changed its graph command since version 7. However there are a lot of c…
 From single event observations to time seriesHi everyone,
As I am quite new to Stata hope you can help me out with the next question.
Currently… From single event observations to time seriesHi everyone,
As I am quite new to Stata hope you can help me out with the next question.
Currently…
 Mundlak procedureDear all,
When using the Mundlak procedure, if one wishes to include year fixed effects (i.e. a dum… Mundlak procedureDear all,
When using the Mundlak procedure, if one wishes to include year fixed effects (i.e. a dum…
 Time and Country Fixed Effects in Panel Data Estimation (OLS and PPMLI am doing a regression taking 34 countries for the time period 2010-19. It is a gravity model so th… Time and Country Fixed Effects in Panel Data Estimation (OLS and PPMLI am doing a regression taking 34 countries for the time period 2010-19. It is a gravity model so th…
Subscribe to:
Post Comments (Atom)
0 Response to Use the lastvar local to read v1 v2 v3 variables from insheet with numeric names
Post a Comment