Announcement

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

  • Destring variables from text to numeric

    I need to destring variables in an excel spreadsheet that will be merged at a later date with another set of data that has been successfully imported from an access database to an excel spreadsheet into Stata. I do not want to use encode because it is giving data errors. I would like to use a different method. I particularly need to destring 'earlyordelayed' and 'causation'. My stata skills are beginner level and I have spent many days on trying to resolve this but find the help files difficult to interpret. Any assistance would be greatly appreciated.
    Click image for larger version

Name:	Stata capture.PNG
Views:	2
Size:	34.2 KB
ID:	1528467


    Attached Files
    Last edited by Pauline Dobson; 12 Dec 2019, 14:54.

  • #2
    First, understanding that -encode- and -destring- are not at all alternatives to one another would help you. Please see
    https://www.stata.com/support/faqs/d...tring-command/
    The choice between encode and destring therefore does not depend on which one is giving errors, but on what you are trying to do.

    You *do* need to use encode for earlyordelayed and causation. If you would concretely show what kind of "errors" encode is giving you, we might be able to help. "Concretely" here means that you copy/paste the encode command that gave you an error, and the error message that Stata gave.

    From the way you have posted your question, I also think that you would benefit from another reading of the StataList FAQ (tab at top left of the StataList screen). That has a lot of advice for new users on how to effectively post a question here.

    Comment


    • #3
      A string variable can only be subjected to -destring- if it is a string representation of a number. So if your string variable had values that looked like "123", "789.654" or the like then you could, and should -destring- it. But a variable whose content is alphanumeric cannot undergo -destring-, as Stata has shown you.

      The correct way to make a numeric version of this variable is to -encode- it. You have stated that you tried this but you are getting "data errors," but since you don't describe what those errors are, nobody can really help you.

      My advice is to post back showing example data using the -dataex- command, and then showing what happens when you apply -encode-, and specifically point out what is wrong with the results you are getting.

      If you are running version 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.

      Added: Crossed with #2 which gives the same advice more succinctly.

      Comment

      Working...
      X