Announcement

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

  • replace if with no real changes made

    Dear Statalist,

    I have a variable State with storage type str3. I want to creat a dummy variable, 1 when State equals "CT", 0 for else. So I generate a new dummy as below:

    gen New_England = 0
    replace New_England = 1 if State == "CT"

    But 0 real changes made.

    I am confused which step is wrong. Forgive me that I may ask a silly easy question.. And thank you for your help!

    Best regard
    Lijuan

  • #2
    The observations may contain spaces. Try

    Code:
    replace State= trim(State)
    replace New_England = 1 if State == "CT"
    If this does not solve it, present a data example using dataex.

    Comment


    • #3
      thank you, Andrew! It works! But could you please explain what "trim" is?

      Comment


      • #4
        strtrim(s)
        Description: s without leading and trailing blanks (ASCII space character char(32)); equivalent to strltrim(strrtrim(s))

        strtrim(" this ") = "this"
        Domain s: strings
        Range: strings without leading or trailing blanks
        So it's a string function that eliminates leading and trailing blanks (spaces). The usual way to find information about commands, functions, etc. is to type <<help commandname>>, for example, in this case

        Code:
        help trim()

        Comment


        • #5
          Andrew Musau I have similar problem. I want to replace values of a string variable from another language to English. I trimed the variable to make sure there is no leading blank space, but still it does not work. It says 0 changes. Here is the variable I would I am working on:


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str38 gender
          "
           اناث" 
          "
           اناث" 
          "
           ذکور" 
          "
           اناث"
          "
           اناث"
          "
           اناث"
          "
           اناث"
          "
           اناث"
          "
           اناث"
          "
           اناث"
          end

          but it says 0 changes made. Is there anything with the data type or unicode that I do not understand?

          Comment


          • #6
            As far as I can see, there are no leading spaces in the string. The first quotation mark is imported in a different observation from the word and more bytes are allocated to the string than needed. Install leftalign from SSC by Robert Picard to change the display format. You can also, in addition:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str38 gender
            "
             اناث"
            "
             اناث"
            "
             ذکور"
            "
             اناث"
            "
             اناث"
            "
             اناث"
            "
             اناث"
            "
             اناث"
            "
             اناث"
            "
             اناث"
            end
            ssc install leftalign, replace
            leftalign gender
            drop if missing(gender)
            recast str10 gender
            browse
            Last edited by Andrew Musau; 02 Dec 2020, 22:27.

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              As far as I can see, there are no leading spaces in the string. The first quotation mark is imported in a different observation from the word and more bytes are allocated to the string than needed. Install leftalign from SSC by Robert Picard to change the display format. You can also, in addition:

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str38 gender
              "
              اناث"
              "
              اناث"
              "
              ذکور"
              "
              اناث"
              "
              اناث"
              "
              اناث"
              "
              اناث"
              "
              اناث"
              "
              اناث"
              "
              اناث"
              end
              ssc install leftalign, replace
              leftalign gender
              drop if missing(gender)
              recast str10 gender
              browse
              Thanks Andrew Musau. This problem is resolved. Your input was helpful!

              Comment

              Working...
              X