Announcement

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

  • Replacing string variable observations to a missing value

    Hi everyone!

    Currently I'm working on a dataset that includes ESG ratings for various firms. Of course these are numeric values, but the ESG variable is currently loaded in Stata as a string because it includes observations with NA (meaning not available). I want to destring this variable, but when I try this it states that the variable includes nonnumeric values, which makes sense as next to numbers it also includes the NAs in the observations. However, when I try to replace these NA values as missing ("." in Stata) it says that NA is not found (see screenshots as to how my data looks and the code I'm currently using).

    What can I do to identify the "NA"s in my ESG data as missing, so that I can convert it to a numeric variable? Hope someone can help me out!

    Kind regards,
    Danielle Berg

    Click image for larger version

Name:	Stata data example.png
Views:	1
Size:	63.5 KB
ID:	1710715
    Click image for larger version

Name:	stata message.png
Views:	1
Size:	26.7 KB
ID:	1710716

  • #2
    You wanted

    Code:
    replace ESG = . if ESG == "NA"
    as NA is a literal string -- not the name of a variable or scalar (which Stata can't find, which is the error). Other way round, ESG is a variable name, which is why you write it without " " but NA isn't a variable (or scalar) name.

    All that said,

    Code:
    destring ESG, ignore("NA") replace 
    should get you there directly, unless there are yet other non-numeric characters.

    Comment

    Working...
    X