Announcement

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

  • Variable creation by concetating variable value with variable name

    This is a bit of a specific request. I am trying to create variable names that concentate the value of one variable with the name of another. Picture 2 separate data files with similarly named variables but which mean different things. I'd like to merge the files together and make sure that the variables are uniquely named.

    Here's an example of what my datasets look like:

    dataset 1:
    District Sequence Number Var1 Var2
    A 1 1111 1222
    B 1 1333 1444
    dataset 2:
    District Sequence Number Var1 Var2 Var3
    A 2 1555 1666 1777
    B 2 1888 1999 2111
    The variable names should be associated with their sequence number, which would create unique variable names. Here's what I want my data to look like:
    Item 1Var1 1Var2 2Var1 2Var2 2Var3
    A 1111 1222 1555 1666 1777
    B 1333 1444 1888 1999 2111
    Basically, I need to figure out how to rename the variables such that they incorporate the sequence number of the dataset.

    Thank you in advance for any help you can provide.

  • #2
    You might get better responses if you post this in the General forum. This forum is for Mata matters.

    Anyway, you could reshape wide . . ., i(District) j(<Sequence Number>) your two datasets and then merge 1:1 District the two datasets. Note that the sequence number will be a suffix of the Var1 Var2 variable names. You cannot begin a variable name with a numeral.

    You could also append the two datasets and then reshape, but you would have all-missing Var31 variable, which you would then drop.
    Last edited by Joseph Coveney; 10 Jan 2018, 19:18.

    Comment


    • #3
      Thank you, Joseph! I didn't think of reshaping the files. This works!

      Comment

      Working...
      X