Announcement

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

  • Removing spaces in string variables

    Hi all,

    I am trying to remove spaces from one of my string variables "idhspid". There are three spaces at the beginning of each observation whereas I would like no spaces. For example:

    " 81801 10051001 2" (3 spaces at the beginning before "81801")
    "81801 10051001 2" (no spaces at the beginning before "81801")

    Please see an example of my dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str23 idhspid int year
    "   81801    10051001  2" 1988
    "   81801    10051002  2" 1988
    "   81801    10051004  2" 1988
    "   81801    10051007  2" 1988
    "   81801    10051008  2" 1988
    "   81801    10051010  2" 1988
    "   81801    10051012  2" 1988
    "   81801    10051014  1" 1988
    "   81801    10051015  2" 1988
    "   81801    10051016  2" 1988
    "   81801    10051018  2" 1988
    "   81801    10141001  2" 1988
    "   81801    10141003  2" 1988
    "   81801    10141004  2" 1988
    "   81801    10141005  2" 1988
    "   81801    10141006  2" 1988
    "   81801    10141007  7" 1988
    "   81801    10141007 11" 1988
    "   81801    10141008  2" 1988
    "   81801    10141009  2" 1988
    "   81801    10141010  2" 1988
    "   81801    10141011  2" 1988
    "   81801    10141012  2" 1988
    "   81801    10141013  2" 1988
    "   81801    10141014  3" 1988
    "   81801    10141015  2" 1988
    "   81801    10141016  2" 1988
    "   81801    10141017  2" 1988
    "   81801    10141019  2" 1988
    "   81801    10141020  2" 1988
    "   81801    10141021  2" 1988
    "   81801    10141022  2" 1988
    "   81801    20011001  2" 1988
    "   81801    20011002  1" 1988
    "   81801    20011003  2" 1988
    "   81801    20011004  2" 1988
    "   81801    20011007  2" 1988
    "   81801    20011007  8" 1988
    "   81801    20011007  9" 1988
    "   81801    20011008  1" 1988
    "   81801    20011009  2" 1988
    "   81801    20011010  2" 1988
    "   81801    20011011  2" 1988
    "   81801    20011012  2" 1988
    "   81801    20011014  3" 1988
    "   81801    20011015  2" 1988
    "   81801    20011016  2" 1988
    "   81801    20011017  2" 1988
    "   81801    20011019  2" 1988
    "   81801    20011020  2" 1988
    "   81801    20011022  4" 1988
    "   81801    20011024  2" 1988
    "   81801    20011027  2" 1988
    "   81801    20011028  2" 1988
    "   81801    20011028  8" 1988
    "   81801    20011029  2" 1988
    "   81801    20011030  2" 1988
    "   81801    20011031  2" 1988
    "   81801    20011033  2" 1988
    "   81801    20011034  2" 1988
    "   81801    20121003  2" 1988
    "   81801    20121004  2" 1988
    "   81801    20121006  2" 1988
    "   81801    20121009  2" 1988
    "   81801    30121001  2" 1988
    "   81801    30121002  2" 1988
    "   81801    30121003  2" 1988
    "   81801    30121004  2" 1988
    "   81801    30121005  1" 1988
    "   81801    30121007  2" 1988
    "   81801    30121008  2" 1988
    "   81801    30121009  2" 1988
    "   81801    30121011  2" 1988
    "   81801    30121012  2" 1988
    "   81801    30121013  2" 1988
    "   81801    30121014  2" 1988
    "   81801    30121015  2" 1988
    "   81801    30121016  2" 1988
    "   81801    30121017  2" 1988
    "   81801    30121018  2" 1988
    "   81801    30121021  2" 1988
    "   81801    30121023  2" 1988
    "   81801    30121024  2" 1988
    "   81801    30121006  2" 1988
    "   81801    30121006  3" 1988
    "   81801    30561001  2" 1988
    "   81801    30561004  2" 1988
    "   81801    30561005  2" 1988
    "   81801    30561007  2" 1988
    "   81801    30561008  2" 1988
    "   81801    30561009  1" 1988
    "   81801    30561009 10" 1988
    "   81801    30561010  2" 1988
    "   81801    30561011  2" 1988
    "   81801    30561013  2" 1988
    "   81801    30561014  2" 1988
    "   81801    30561017  2" 1988
    "   81801    30561018  2" 1988
    "   81801    30561020  2" 1988
    "   81801    30561023  2" 1988
    end
    label values year YEAR
    label def YEAR 1988 "1988", modify

    I can do this manually one by one but with over 20,000 observations I am hoping there is an easier way.

    I have seen this post: https://www.statalist.org/forums/for...tring-variable but it does not provide exactly what I need.

  • #2
    I have managed to figure it out! This the code I used in case it benefits anyone else:

    Code:
    replace idhspid = trim( idhspid )
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str23 idhspid int year
    "81801    10051001  2" 1988
    "81801    10051002  2" 1988
    "81801    10051004  2" 1988
    "81801    10051007  2" 1988
    "81801    10051008  2" 1988
    "81801    10051010  2" 1988
    "81801    10051012  2" 1988
    "81801    10051014  1" 1988
    "81801    10051015  2" 1988
    "81801    10051016  2" 1988
    "81801    10051018  2" 1988
    "81801    10141001  2" 1988
    "81801    10141003  2" 1988
    "81801    10141004  2" 1988
    "81801    10141005  2" 1988
    "81801    10141006  2" 1988
    "81801    10141007  7" 1988
    "81801    10141007 11" 1988
    "81801    10141008  2" 1988
    "81801    10141009  2" 1988
    "81801    10141010  2" 1988
    "81801    10141011  2" 1988
    "81801    10141012  2" 1988
    "81801    10141013  2" 1988
    "81801    10141014  3" 1988
    "81801    10141015  2" 1988
    "81801    10141016  2" 1988
    "81801    10141017  2" 1988
    "81801    10141019  2" 1988
    "81801    10141020  2" 1988
    "81801    10141021  2" 1988
    "81801    10141022  2" 1988
    "81801    20011001  2" 1988
    "81801    20011002  1" 1988
    "81801    20011003  2" 1988
    "81801    20011004  2" 1988
    "81801    20011007  2" 1988
    "81801    20011007  8" 1988
    "81801    20011007  9" 1988
    "81801    20011008  1" 1988
    "81801    20011009  2" 1988
    "81801    20011010  2" 1988
    "81801    20011011  2" 1988
    "81801    20011012  2" 1988
    "81801    20011014  3" 1988
    "81801    20011015  2" 1988
    "81801    20011016  2" 1988
    "81801    20011017  2" 1988
    "81801    20011019  2" 1988
    "81801    20011020  2" 1988
    "81801    20011022  4" 1988
    "81801    20011024  2" 1988
    "81801    20011027  2" 1988
    "81801    20011028  2" 1988
    "81801    20011028  8" 1988
    "81801    20011029  2" 1988
    "81801    20011030  2" 1988
    "81801    20011031  2" 1988
    "81801    20011033  2" 1988
    "81801    20011034  2" 1988
    "81801    20121003  2" 1988
    "81801    20121004  2" 1988
    "81801    20121006  2" 1988
    "81801    20121009  2" 1988
    "81801    30121001  2" 1988
    "81801    30121002  2" 1988
    "81801    30121003  2" 1988
    "81801    30121004  2" 1988
    "81801    30121005  1" 1988
    "81801    30121007  2" 1988
    "81801    30121008  2" 1988
    "81801    30121009  2" 1988
    "81801    30121011  2" 1988
    "81801    30121012  2" 1988
    "81801    30121013  2" 1988
    "81801    30121014  2" 1988
    "81801    30121015  2" 1988
    "81801    30121016  2" 1988
    "81801    30121017  2" 1988
    "81801    30121018  2" 1988
    "81801    30121021  2" 1988
    "81801    30121023  2" 1988
    "81801    30121024  2" 1988
    "81801    30121006  2" 1988
    "81801    30121006  3" 1988
    "81801    30561001  2" 1988
    "81801    30561004  2" 1988
    "81801    30561005  2" 1988
    "81801    30561007  2" 1988
    "81801    30561008  2" 1988
    "81801    30561009  1" 1988
    "81801    30561009 10" 1988
    "81801    30561010  2" 1988
    "81801    30561011  2" 1988
    "81801    30561013  2" 1988
    "81801    30561014  2" 1988
    "81801    30561017  2" 1988
    "81801    30561018  2" 1988
    "81801    30561020  2" 1988
    "81801    30561023  2" 1988
    end
    label values year YEAR
    label def YEAR 1988 "1988", modify

    Comment


    • #3
      On the contrary, the trim() function mentioned in that thread seems exactly right for your problem.

      — as you realized.

      Comment


      • #4
        Yes fortunately I quickly realised after posting, thank you Nick

        Comment

        Working...
        X