Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72764

Rename variable names before import

$
0
0
I have a collection of xls files that I want to append. Each has a set of indicators for a group of countries and years.
The layout of each is in wide format with years (numerical) as column headers (i.e., Country, indicator name, and then years e.g., 1990, 1991 etc.; see also
If I import from excel, using first row as var names, Stata replaces the numerical 1970 with capital letters.

I cannot append ignoring the first row or with the option 'extvarlist', because some of these files range 1960-2010, or 1980-2014, etc.

What I would like is Stata to rename all variables X to yrX before importing, or something similar.
It doesnt really matter if none year variables are also included; there's only a few, which I could rename with a few simple lines.


Any thoughts?
I will need my data long at some point anyway, so it might be okay to reshape individual files, save them, then merge.
Problem is I don't know how to reshape with data as below (I dont know whta to use as 'j').
I did notice Stata sticks the year numbers into the value labels when importing with 'firstrow' specified, so maybe there is a way to batch rename variables using 'yr+label'?


Hope this is clear enough.
Thanks for your help.

using option firstrow when importing:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str30 Country str3 CTRYCODE str34 Indicator double(E F G)
"Afghanistan"    "AFG" "TechPot_Wind_MWh" 3653718937.2448316 3653718937.2448316 3653718937.2448316
"Albania"        "ALB" "TechPot_Wind_MWh"  222818627.4862292  222818627.4862292  222818627.4862292
"Algeria"        "DZA" "TechPot_Wind_MWh" 20192854481.295692 20192854481.295692 20192854481.295692
"American Samoa" "ASM" "TechPot_Wind_MWh"                  .                  .                  .
"Andorra"        "ADO" "TechPot_Wind_MWh"                  .                  .                  .
end
label var Country "Country" 
label var CTRYCODE "CTRY CODE" 
label var Indicator "Indicator" 
label var E "1970" 
label var F "1971" 
label var G "1972"
Or without firstrow specified

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str30 A str9 B str36 C str34 D double(E F G)
"Country"        "CTRY CODE" "Region"                             "Indicator"                      1970               1971               1972
"Afghanistan"    "AFG"       "South, East and South-Eastern Asia" "TechPot_Wind_MWh" 3653718937.2448316 3653718937.2448316 3653718937.2448316
"Albania"        "ALB"       "Europe"                             "TechPot_Wind_MWh"  222818627.4862292  222818627.4862292  222818627.4862292
"Algeria"        "DZA"       "Africa"                             "TechPot_Wind_MWh" 20192854481.295692 20192854481.295692 20192854481.295692
"American Samoa" "ASM"       "Oceania"                            "TechPot_Wind_MWh"                  .                  .                  .
end

Viewing all articles
Browse latest Browse all 72764

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>