Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reshape: Using previous variable labels to create a new string variable?

    Hey everyone,

    I have the following dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str14 Type float(ag1de_Plcyprob ag1de_PlcyForm ag1de_Implemen ag1de_Affected ag1de_PlcyLear ag1de_External obs_id)
    "ag2di_Admin"    63.15789 94.73685      100  63.15789  47.36842 68.42105 1
    "ag2di_Cooperat"       50       75      100        50        25      100 2
    "ag2di_Economic" 44.73684 85.52631 94.73685  59.21053  28.94737 78.94737 3
    "ag2di_Informat" 66.66667 83.33333      100 33.333336        50 83.33333 4
    "ag2di_Regulve"        60       88 98.66667        64 34.666668 53.33334 5
    end
    And am reshaping it using this command:

    Code:
    reshape long ag1de_, i(obs_id) j(Category) string
    Here's my question: I would like the new variable created "Category" to not consist of the previous variables names (e.g. "PlcyLear") but of the variable labels of the original ag1de_ prefixed variables (e.g. "Policy learning"). Do you have an idea how I could do this?

    For your reference, here are the policy labels.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str14 name str25 varlab
    "Type"           ""                        
    "ag1de_Plcyprob" " Policy problem/issue"    
    "ag1de_PlcyForm" " Policy Formulation"      
    "ag1de_Implemen" " Implementation Structure"
    "ag1de_Affected" " Affected Groups"        
    "ag1de_PlcyLear" " Policy learning"        
    "ag1de_External" " External Factors"        
    "obs_id"         ""                        
    end

    Thank you!

  • #2
    I would take #2 (your second code example) and edit it to a do-file with lines like

    Code:
     label var ag1de_Plcyprob "Policy problem/issue"
    Last edited by Nick Cox; 12 Dec 2024, 07:19.

    Comment


    • #3
      Thanks for the quick response, Nick.

      That means there is no way automate this with a local and foreach similar to what one does to keep variable labels after collapse? I have attempted this myself, but couldn't get there. I'm trying to do this for a range of variables and later on graphs, hence a more automated way would be great. But fine if not possible.

      Thanks!

      Comment


      • #4
        There are indeed other ways to do it. It is a good question: would a loop make it faster or what other virtue would it have?

        With your second dataset the single command line

        Code:
         gen wanted = "label var " + name + " " + char(34) + trim(varlab) + char(34)
        puts the text of a do-file into the variable wanted, which you can then export and run.

        The most esoteric trick there is using char(34) to insert a double quotation mark, which could be done more awkwardly otherwise. But the device is just the same whatever number of variables you have.

        In any good text editor or scripting language there should be something similar using some command language.

        Otherwise put, post #1 asks for how to combine information in two datasets.

        You could add the variables in the second dataset to those in the first and then write a loop if you wished.

        So if we merged 1:1 on observations -- assuming no conflict of variable names -- the loop might be something like

        Code:
        count if name != "" 
        
        forval i = 1/`r(N)'  { 
               capture label var `=name[`i']'  "`= trim(varlab[`i'])'" 
        }
        I have not tested any of that, but I will join anyone in calling it tricky to get right (if I did).

        Comment


        • #5
          See also another thread today https://www.statalist.org/forums/for...th-information in which Maarten Buis maps a dataset to a do-file.

          Comment


          • #6
            Thanks a lot Nick! Your suggestion of merging the two datasets was excellent and sufficient for me. I just included the merge in my overall loop. This did what I needed and I didn't need to try the final code you had suggested.

            Comment

            Working...
            X