Hi, I am trying to automate the definition of value labels using 3 variables within a dta: varname (the variable to be labelled), code (the variable value), parameter (the label). Once defined, I plan to use these to label a different dta. I struggling to automate the label definitions, I wondered if there was a way to save as a local the value of the nth observation of a specific variable within a group?
EXAMPLE DATASET:
From this dataset I would want to save the following label lists:
I have an idea of how I can go about it, but it relies on me referring to the value of code and parameter that correspond to the nth observation within each group, which I'm unsure how to do. I also want to be able to extract the value of the varname variable corresponding to each group so that I can name my value label definition accordingly, allowing me to easily automate labelling later on.
I'd be really grateful if anybody had any tips! Thank you so much in advance 
EXAMPLE DATASET:
Code:
clear input str4 varname code str8 parameter var1 1 label1_1 var1 2 label1_2 var1 3 label1_3 var1 4 label1_4 var1 5 label1_5 var1 6 label1_6 var2 0 label2_0 var2 1 label2_1 end
Code:
label define var1labels 1 "label1_1" 2 "label1_2" 3 "label1_3" 4 "label1_4" 5 "label1_5" 6 "label1_6" label define var2labels 0 "label2_0" 2 "label2_1"
Code:
egen grp = group(varname) sort grp code levelsof grp, local(groups) // create local with all unique group values egen grp_count = count(varname), by(grp) // count obs per grp foreach grpnum of local groups { // loop through each vargrp count if grp == `grpnum' local grpcount = r(N) di "Group `grpnum'; Count: `grpcount'" // check working local list = "label define `grpnum'" // ideally want to extract varname value, not `grpnum' forval i = 1/`grpcount' { // local codeval = code[`i'] // something like this? // local parameterval = parameter[`i'] // local list = `list' + " `codeval' `parameterval'" // planning to iteratively build the label definition // di "`list'" } }

Comment