Announcement

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

  • Using wildcards for variable list in reshape long

    Dear Statalists,

    I have a large number of variables (say, 100) with similar names and I would like to reshape my data long. Is there any how I can use wildcards, so I do not have to list all 100 variables in the reshape command?

    Assume, the following is my data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(x v_T1_1 v_T1_2 v_T1_3 v_T1_4 v_T2_1 v_T2_2 v_T2_3 v_T2_4)
    1 13 10  8 14 15 6 17 18
    2 13  7 18 12 14 7 18 18
    end

    I can reshape the data set long by typing reshape long v_T1_ v_T2_, i(x) j(y) and reach the following result:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float x byte y float(v_T1_ v_T2_)
    1 1 13 15
    1 2 10  6
    1 3  8 17
    1 4 14 18
    2 1 13 14
    2 2  7  7
    2 3 18 18
    2 4 12 18
    end


    However, since I have a lot of variables it would be more convenient if it was possible to automatize the process. I am thinking of something along the lines reshape long v_??_ v_??_, i(x) j(y). Using wildcards works for me when I reshape wide (in similar situations) but when trying to reshape long (in this case), it doesn't.

    How would one use wildcards in such a situation? And if not possible, is there a way around this with a local variable list maybe?

    Thank you very much for your input.

    Best wishes,
    Milan

  • #2
    I do not know of a generalizable approach to this problem. One cal usually devise an ad hoc trick that exploits some commonality in the variable names. For example, it appears that the variable names you are working with here all match the wildcard v_??_. In particular, except fot the suffix part that will become the variable y, they are all five characters long. So you can take advantage of that by the following code:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(x v_T1_1 v_T1_2 v_T1_3 v_T1_4 v_T2_1 v_T2_2 v_T2_3 v_T2_4)
    1 13 10  8 14 15 6 17 18
    2 13  7 18 12 14 7 18 18
    end
    
    local stublist
    ds v_??_*
    foreach v of varlist `r(varlist)' {
        local vv = substr("`v'", 1, 5)
        local stublist `stublist' `vv'
    }
    local stublist: list uniq stublist
    
    reshape long `stublist', i(x) j(y)

    Comment

    Working...
    X