Announcement

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

  • Create some new records and assign value based on some variables' value

    Hello all,
    I'm using Stata to clean and arrange the data.

    I would like to "clone" some records, extract the values from some certain variables, in this case "Function 1, Function 2" and assign them into "Qty" variable of 2 new records
    It's quite hard to explain by word, so I try to use below image to show the idea.

    The yellow part will be the new records that expected to be added into the data.

    Click image for larger version

Name:	stata_Question.jpg
Views:	1
Size:	47.9 KB
ID:	1655844


    I hope someone could hep!! really appreciated!

    Quang.

  • #2
    What you show might make sense in the context of a spreadsheet (which is evidently where it was taken from), but as a Stata dataset the result would be malformed, mostly unworkable, and a trap for serious errors when you start to do data analysis. For a valid Stata data set, you should create the two new observations and eliminate the original. The process is called -reshape-ing the data, and once you have the data in Stata, it will go something like this:

    Code:
    gen long obs_no = _n
    drop qty
    reshape long function, i(obs_no) j(qty)
    Note: This assumes that the variables function1 and function2 appear as numeric variables once you import the data to Stata.

    Rather than making people guess about what your Stata data set will be, in the future, always show example data from your Stata data set, not from a spreadsheet or text file from which you hope to import it, and use the -dataex- command to do that. (Unless your question is specifically about how to import the data into Stata, of course!) If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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.

    Comment

    Working...
    X