Announcement

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

  • if [varname] = [values]?

    Hello,

    I have a variable, date that is empty.
    and a series of other date variables that contain date data: ecdate bcdate mhdate...
    I also have a string variable, prefix, that contains string values such as: ec, bc, mh ...

    How can I replace date with the value in one of the other date variables (i.e. ecdate bcdate mhdate...) for rows where the variable name begins with the value of prefix?

    I hope I explained clearly enough.

    Thank you!

  • #2
    Code:
    // HERE'S A TOY DATA SET
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(ecdate bcdate mhdate) str2 prefix float date
    21179 21217 21255 "ec" .
    21248 21171 21305 "bc" .
    21198 21188 21194 "bc" .
    21210 21260 21491 "mh" .
    21399 21491 21474 "mh" .
    end
    format %td ecdate
    format %td bcdate
    format %td mhdate
    
    levelsof prefix, local(prefixes)
    foreach p of local prefixes {
        replace date = `p'date if prefix == "`p'"
    }
    format date %td
    In the future, please provide your own example data when asking for code. It is a fair bet that the toy data set I created here is similar to your own, but if not, then we have both wasted our time. Even the best, most meticulous descriptions of data sets are no substitute for an example provided using the -dataex- command. If you are running version 15.1 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- 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.

    Also, it is best not to use spreadsheet terminology such as "rows" (or columns) when speaking of Stata datasets. Stata's data model is nothing at all like a spreadsheet, despite the superficial appearance as seen in the Browser window. Your data management instincts honed on spreadsheets will rarely be helpful when working with Stata, and will often lead you far astray. To avoid confusing the two in your thoughts, it is best to use different language. In Stata we speak of observations and variables instead of rows and columns.

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

    Comment


    • #3
      Thanks Clyde, your code worked exactly how I needed.

      And I appreciate the help to be a better Stata poster. I hope to communicate questions as clearly as I can and -dataex- (and refining my terminology) will surely help.

      Thanks again,
      Reese

      Comment

      Working...
      X