Announcement

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

  • String Variable Merge

    Hi, I am fairly new to stata and am having difficulty creating a string variable identifying year and quarter eg. 2012Q1, in order to merge two data sets together.

    Currently I have gotten as far as getting the month and year as variables using:

    gen month1 = substr(date, 3, 3)
    gen year1 = substr(date, 6, 4)
    destring year1, replace
    replace month3 = 1 if month1 == "jan"
    replace month3 = 2 if month1 == "feb"
    replace month3 = 3 if month1 == "mar"
    replace month3 = 4 if month1 == "apr"
    replace month3 = 5 if month1 == "may"
    replace month3 = 6 if month1 == "jun"
    replace month3 = 7 if month1 == "jul"
    replace month3 = 8 if month1 == "aug"
    replace month3 = 9 if month1 == "sep"
    replace month3 = 10 if month1 == "oct"
    replace month3 = 11 if month1 == "nov"
    replace month3 = 12 if month1 == "dec"

    I now need to create a quarter variable as a string eg Q1 for month3 = 1 | 2 | 3... and then combine this with the year to create a variable matching the one in the other data set so as to merge. The switching between string and numeric is causing some difficulties and I am not sure where to go from here

    thanks in advance for any advice
    Last edited by Oliver Gassor; 06 Mar 2020, 11:19.

  • #2
    Welcome to Statalist.

    I'm sorry to tell you that you are taking the wrong approach to this problem. When working in Stata, you should not have string variables for dates and times. When you do have string variables containing date and time information you should use them only to create numeric variables containing Stata Internal Format date or time vales, and then use those numeric variables going forward with your analysis.

    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    After familiarizing yourself with Stata dates and times, if you have questions or problems in applying the techniques to this task, please take the time to provide samples of the relevant variables from each of your datasets. Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with your description, and each would require a somewhat different approach. In order to get a helpful response, you need to show some example data.

    Be sure to use the dataex command to do this. If you are running version 15.1 or later, or a fully updated version 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 and 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.

    When asking for help with code, always show example data. When showing example data, always use dataex.

    Comment

    Working...
    X