Announcement

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

  • remove spaces in a dofile

    Hello,
    I'm using stata 14.
    I copied commands from excel to paste them in my dofile. But when I run, there is no change in my data but if I remove the spaces and run again it becomes correct.
    Now I would like to know if there is a way to remove all the spaces at the same time because the number of line is important.
    Thanks and best regards

  • #2
    I don't understand.
    Edit: Why are commands in an excel file? Why not just have them in a text file, if nothing else?


    And also, the number of lines can't be important. Stata doesn't care if the do file is 2 lines or 2000. If the syntax is correct, it'll run.
    Last edited by Jared Greathouse; 04 Jul 2022, 07:54.

    Comment


    • #3
      If part of the contents of the problem do-file were shown here, that would greatly increase the chances of receiving a good answer. In doing that, the O.P. should very literally paste that code into a message and surround it with code delimiters per item 12.3 of the StataList FAQ. It would also be useful to see a similar copy of the code "with the spaces removed."

      I can imagine some reasons why one might prepare code in an Excel file, as it can be a useful way to have someone with spreadsheet but not text processor experience type in long lists of -label- commands (and perhaps other things I haven't thought of). However, I'd agree with Jared that using a spreadsheet in this way is relatively unusual, and if we were told why that is being done, we might have some ideas of nicer ways to accomplish the goal at hand.

      Comment


      • #4
        I can only speculate, but it's likely an idiosyncrasy of metadata that's being copied from excel to the do file editor.

        Comment


        • #5
          Thank you Jared and Mike for your comments.
          below, I have copied a part of my dofile. It was actually easier to do this on excel because it took me less time and I was just incrementing the cells. But when I copied them back to my dofile and run it, there is no change except when I remove the spaces on the characters put between quotation marks like on the first two lines of my example below.
          Thanks

          replace a3_pl = "100" if a3_pl == "BABA GARAGE"
          replace a3_pl = "101" if a3_pl == "GAWANE "
          replace a3_pl = " 102 " if a3_pl == " KEUR SAMBA KANE "
          replace a3_pl = " 103 " if a3_pl == " NGOGOM "
          replace a3_pl = " 104 " if a3_pl == " NGOYE "
          replace a3_pl = " 105 " if a3_pl == " REFANE "
          replace a3_pl = " 106 " if a3_pl == " GADE ESCALE "
          replace a3_pl = " 107 " if a3_pl == " KEUR NGALGOU "
          replace a3_pl = " 108 " if a3_pl == " NDANKH SENE "
          replace a3_pl = " 109 " if a3_pl == " NDINDY "
          replace a3_pl = " 110 " if a3_pl == " NDOULO "
          replace a3_pl = " 111 " if a3_pl == " NGOHE "
          replace a3_pl = " 112 " if a3_pl == " PATAR " & region == 1 & departement == 2
          replace a3_pl = " 113 " if a3_pl == " TAIBA MOUTOUPHA "
          replace a3_pl = " 114 " if a3_pl == " TOCKY GARE "
          replace a3_pl = " 115 " if a3_pl == " TOURE MBONDE "
          replace a3_pl = " 116 " if a3_pl == " DIOFIOR "
          replace a3_pl = " 117 " if a3_pl == " DIOUROUP "
          replace a3_pl = " 118 " if a3_pl == " FIMELA "

          Comment


          • #6
            If I understand you correctly, the leading and trailing blanks around such things as " 118 " or " FIMELA " were created when you pasted the material from Excel, and, perhaps needless to say, this would make that code not do what you want in Stata, since (for example) " FIMELA " is not the same thing to Stata as "FIMELA". Blanks are simply another character to Stata when it looks at a string value, so if a3_pl == " FIMELA " will not match observations containing "FIMELA" I don't know why this happens when you paste from Excel, as I can't reproduce what you describe. It's possible that those strings had padding blanks inserted in Excel, or that when pasted into Stata's do file editor, the column separators implicit in Excel were translated to blanks or tabs, per Jared's comments.
            It's of course possible you might have entered or formatted the material differently in Excel than I did. (That's really an Excel issue I don't want to get into.) I'd note that it's possible to find and replace all instances of -" - with -"- (quote space with quote) by using the find/replace feature of the do-file editor.

            Incidentally, not having the code pasted between code delimiters as I suggested turned out not to make a difference to my response in this case, but I still in general would recommend reading and following the StataList FAQ.

            Finally, I'd note that there are other ways to do what you want to do with that collection of replace statements, such as use some kind of loop, but I don't think that would be particularly easier or clearer. However, if you have a very long set of replace statements (say 50 or 100), there might be some merit in creating a file with those string to number correspondences, and using -merge- to put them onto your file.

            Comment


            • #7
              Thank you very much Mike.
              Regards

              Comment

              Working...
              X