Announcement

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

  • New reshape module on the SSC: reshapelabel

    reshapelabel is a program that reshapes a long dataset wide, where the j variable can be a string with spaces (or other characters that are typically not permitted as part of a variable name). The program will encode the entered j variable, generating a temporary numeric variable that gets reshaped. However, the program preserves the original strings in a local and automatically applies them to the new variables' labels. The only restriction for the j variable is now that it must adhere to the restrictions of a variable label.

    Here is some example usage of the program.
    Code:
    input str6 firm str3 month int sales
    "Firm A" "Jan" 10
    "Firm A" "Feb" 12
    "Firm B" "Jan"  8
    "Firm B" "Feb"  9
    end
    reshapelabel, metric(sales) jvar(firm) ivar(month)
    In an ordinary reshape wide, even after specifying option string, this would have thrown an error, because the j variable (firm) contains spaces, which a variable name cannot have. The code above will produce a dataset with three variables: month, sales1 (labelled as Firm A), sales2 (labelled as Firm B). The code preserves the original sort order, so the entry for "Jan" will be before the entry for "Feb". This program has only been tested on version 18.5 but I suspect it will work on earlier versions that support reshape.

  • #2
    Interesting package. Probably useful to relatively few, but to those few, very.

    Comment


    • #3
      Actually I can imagine using this as I reshape a lot in preparing graphs with text labels.

      Comment


      • #4
        I am bit puzzled by the example at least.

        The example to me looks in the best layout (see below *) for just about all Stata purposes for panel or longitudinal data with different panels and different times.

        The main problem to me is not one of layout but here that holding identifier as a string variable is not ideal, so then the simplest solution is first to run encode or egen, group() to get a numeric variable with value labels as panel identifier, which you need any way for many panel or longitudinal purposes as anything depending on a previoustsset or xtset requires a numeric identifier.

        Holding dates (here months of the year) is even more limiting and the bullet to bite is to define value labels for the months and then encode the string date variable too.

        You can do that in a clever way or in a simpler way and the simpler way is not much more work, just a little tedious.

        Code:
        . tokenize "`c(Months)'"
        
        . forval m = 1/12 {
          2. label define month `m' "``m''", add
          3. }
        
        . label list month
        month:
                   1 January
                   2 February
                   3 March
                   4 April
                   5 May
                   6 June
                   7 July
                   8 August
                   9 September
                  10 October
                  11 November
                  12 December
        
        . label def Month 1 "January" 2 "February"
        You get the idea of the last command: clearly you need to extend the code to the other 10 months. And c(Months) is not much use if you are using different month names, e.g. in a language other than English.

        The use case seems that you really want wide layout in which case
        Code:
        encode
        is not enough of a solution, as the value labels it creates won't become variable labels.

        Code:
        clear 
        
        input str6 firm str3 month int sales
        "Firm A" "Jan" 10
        "Firm A" "Feb" 12
        "Firm B" "Jan"  8
        "Firm B" "Feb"  9
        end
        
        encode firm, gen(Firm)
        
        drop firm 
        
        reshape wide sales, i(month) j(Firm)
        So, you would need to catch the value labels before they disappear and put them back as variable labels. That is programmable but this package seems to have done it.

        But why would you prefer wide layout? Genuinely intrigued.

        (*) I have learned from Clyde Schechter here that layout is a great word for long layout, wide layout and so forth. The problem with format as a termis that it is so overloaded -- if not in Stata, then beyond. I count file format and display format as entrenched uses, but data format is not a better term to me than layout and variable format as a loose synonym for variable or storage type is better avoided.

        Comment

        Working...
        X