Announcement

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

  • #16
    Patrick:
    as an aside to Hemanshu's helpful guidance, check if your -"n.a."- has leading and/or trailing blanks.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #17
      Indeed, just in case leading/trailing blanks and/or capitalisation are the issue here, you could tweak my code to the following:
      Code:
      foreach var of varlist Interest_Revenue-Equity {
          capture confirm string var `var'
          if !_rc {
              replace `var' == "" if lower(trim(`var')) == "n.a."
          }
      }

      Comment


      • #18
        Hemanshu Kumar, the "n.a." in my dataset are only in the numeric variables and not the string variables.
        Please I have tried several times just now to use dataex command to extract some few variables and 10 observations which include those with "n.a." from my dataset but unfortunately, dataex command modifies the output by filling all the "n.a." with the average of each column I think. Please Nick Cox can confirm or deny since he is one of the authors of dataex in stata. In view of this issue, Hemanshu Kumar, please is there any other means that I can share my dataset for assistance?

        Comment


        • #19
          The only way that "n.a." can appear in a listing of a numeric variable is if it is a value label.

          The impression that dataex will replace values with some kind of average makes no sense to me.

          The Catch-22 here should be clear: the only way we can judge what you claim about dataex output is if you show it to us.

          Comment


          • #20
            Carlo Lazzaro, there are leading and trailing blanks in the dataset and from the data editor after importing the dataset, there are leading blanks in the string variables and trailing blanks in the numerical variables which include the "n.a." thanks.
            Last edited by Patrick Donkor; 20 Nov 2022, 19:01.

            Comment


            • #21
              For others who have been following this thread:

              Patrick contacted me on private message with his dataset, and the solution was very similar to what I had suggested here. I am still not clear on why the solution here did not work, but at any rate, the problem is now sorted out.

              Patrick's original data is in Excel format. After reading it into Stata, here is what a small extract looks like:

              Code:
              . dataex NetInterestRevenuethEUR2020 - OperatingrevenuesthEUR2020 in 1/5
              
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input str17(NetInterestRevenuethEUR2020 Netinterestincomeexpenseth) str18 OperatingrevenuesthEUR2020
              "19286000" "19286000" "41779000"
              "n.a."     "n.a."     "n.a."    
              "11818000" "11818000" "20500000"
              "31994000" "31994000" "46717000"
              "10473000" "10473000" "22113000"
              end
              And thus here is the solution that worked:

              Code:
              import excel using "path_to_excel_file", firstrow clear
              
              foreach var of varlist * {
                  capture confirm string var `var'
                  if !_rc {
                      replace `var' = "" if `var'=="n.a."
                      destring `var', replace
                  }
              }
              p.s. indeed there was an error in my solution in #13 and #17, in the double equal sign in
              Code:
              replace `var' == "" if ...
              But paradoxically, in #14, OP stated "I didn't get any error message after executing the command, the "n.a." were not deleted", so I didn't catch that syntax error earlier.
              Last edited by Hemanshu Kumar; 21 Nov 2022, 09:00.

              Comment


              • #22
                Please with the help of Hemanshu Kumar, I've been able to clean all the "n.a." in my dataset following his last code in this thread. Thanks to all of you for all your efforts and assistance.
                Last edited by Patrick Donkor; 22 Nov 2022, 12:09.

                Comment

                Working...
                X