Skip to content

Instantly share code, notes, and snippets.

@isaacgeng
Last active January 30, 2022 01:36
Show Gist options
  • Save isaacgeng/0bcfc1708895ee1c95791c0375d19e8c to your computer and use it in GitHub Desktop.
Save isaacgeng/0bcfc1708895ee1c95791c0375d19e8c to your computer and use it in GitHub Desktop.
// label vars using 1st row, then drop 1st row
foreach var of varlist * {
label variable `var' "`=`var'[1]'"
replace `var'="" if _n==1
}
drop in 1
// wrap into a program
cap program drop mapvalues
program define mapvalues
syntax varlist, a(string) b(string)
local n: word count `a'
foreach var in `varlist'{
forvalues i = 1/`n'{
local a_t : word `i' of `a'
local b_t : word `i' of `b'
replace `var' = "`b_t'" if `var' == "`a_t'"
}
destring `var', replace
}
local clear
end
// begin mapping
mapvalues B-J, a("沒有 1-2次 3-4次 5-6次 7次或以上") b("1 2 3 4 5")
clear
numlist "1/2 34/56 678/901"
tokenize `r(numlist)'
local N : word count `r(numlist)'
set obs `N'
gen id = .
forval i = 1 / `N' {
qui replace id = ``i'' in `i'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment