mcaceresb / stata-gtools

Faster implementation of Stata's collapse, reshape, xtile, egen, isid, and more using C plugins
https://gtools.readthedocs.io
MIT License
182 stars 38 forks source link

Add a new feature to greshape : set variable labels as values after greshape gather #55

Closed elektrotiko closed 5 years ago

elektrotiko commented 5 years ago

Hello

Please, and if possible, add a new feature to greshape gather as suggested subsequently.

I would like to see a new option for greshape gather, say "varlabasvalue" to be able to keep variable labels instead of variable names in the keys variable "variable", which is the case now after greshape gather.

As it is now, I am doing this manually in the following way before reshape:

BEFORE GRESHAPE GATHER:

***!!!!!!!!!! EXTRACT BOTH VARIABLE NAMES AND LABELS BEFORE RESHAPE 
foreach v of varlist * {

    local `v'_varname : permname `v'  // extract the name of variable
    display as result "``v'_varname'"
    *****
    local `v'_varlabel : var label `v' // extract the label of variable
    display as result "``v'_varlabel'"

 }

DO THE GRESHAPE GATHER:

greshape gather Alias*, values(values) keys(variable)

AFTER greshape gather


***!!!! APPLY SAVED LOCALS as values AFTER GRESHAPE GATHER

levelsof variable, local(Alias)

foreach v of local Alias {

    //* DISPLAY CURRENT LEVEL OF ALIAS
    display "`v'"
    // DISPLAY IN RED THE SAVED LOCAL OF VARNAME
    display as error "``v'_varname'"
    //* STRIP THE LAST CHARACTER "1" FROM THE SAVED VARNAME LOCAL -> do not know how this has been added to begin with
    local `v'_varname = substr("``v'_varname'",1,length("``v'_varname'")-1) // strip that "1" that has been added to my local macro without my knowledge
    //* SHOW THE RESULT OF STRIP
    display as error "This is the right version of saved local macro of variable name : ``v'_varname'"
    //* DISPLAY AS RESULT IN BOLD THE SAVE VARIABLE LABEL
    display as result "``v'_varlabel'"
    //* REPLACE variable with the saved variable label for current Alias value
    replace variable = "``v'_varlabel'" if variable=="`v'"

}

The above chunks of codes are just to give you an idea about how I am thinking. I am pretty sure you will be able to add options for setting variable labels as values after greshape gather or greshape wide in a much better way than my way of thinking above.

mcaceresb commented 5 years ago

This is in the develop branch; uselabel will replace the variable names with their labels if applicable. Will merge to master this week; will leave open until then.