Announcement

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

  • Generating a variable that is a combination of other variables

    Hi! I want to create a variable that combines other variables, regardless of their type. For instance, given Old1 and Old2, I want to be able to create the variable 'New' as follows:
    Old 1 Old 2 New
    FE 32 FE32
    ML 21 ML21
    XC ub XCub
    12 AP 12AP
    What command/code can I use to do this successfully?
    Thank you!!

  • #2
    -help egen- and look at the -concat()- function therein.

    That said, your question is bit strange because both Old1 and Old2 will be string variables when you import them into Stata. The 32, 21, and 12 will not import to Stata as numeric, because they are in the same columns of your spreadsheet as non-numeric material, so Stata imports the entire column as a string variable. So you will not actually confront the question you ask. Consequently, once you have brought this data into Stata, you will not even need -egen, concat()-. You will just be able to write -gen new = old1 + old2-.

    In the future, please import your data into Stata before posting questions here (unless the question is actually about importing the data into Stata.) Once you have a data set, the best way to show example data is to use the -dataex- command. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
    Last edited by Clyde Schechter; 15 Nov 2018, 10:53.

    Comment

    Working...
    X