Announcement

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

  • trim string variables

    Hi,

    I have trailing spaces in several of my string vars. I tried:

    strrtrim("schname")

    and get the following error message:

    command strrtrim is unrecognized.

    Is this command no longer available?

  • #2
    See the help for functions or http://www.stata.com/manuals14/fn.pdf p.138

    strrtrim() is a function, not a command, and so can be issued only as a part of a command

    As applied to the literal string "schname" it would do nothing as there are no trailing blanks.

    You may need something more like

    replace schname = strrtrim(schname)

    Comment


    • #3
      Thanks!

      Comment


      • #4
        Hi all,

        I'm struggeling to use the -ltrim- function correctly.

        I have leading spaces for all countries in my country variable, hence I tried using
        Code:
        replace country= ltrim("")
        However, this just made all the country observations disappear. I'm looking for a solution where I can apply the -ltrim- function for all country observations at the same time, as I have paneldata for 195 countries in 117 years.

        Thank you in advance for your help!

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int year str25 country float rainfallmm
        1901 " Afghanistan"              20.910303
        1901 " Albania"                  103.67812
        1901 " Algeria"                   9.454398
        1901 " American Samoa"            215.5257
        1901 " Andorra"                   72.60929
        1901 " Angola"                    77.58418
        1901 " Antigua & Barbuda"     253.1407
        1901 " Argentina"                 34.57885
        1901 " Armenia"                   48.49679
        1901 " Aruba"                      65.3903
        1901 " Australia"                  34.7993
        1901 " Austria"                   95.29636
        1901 " Azerbaijan"                35.68129
        1901 " Bahrain"                   3.678049
        1901 " Bangladesh"               202.73495
        1901 " Barbados"                 170.03625
        1901 " Belarus"                   50.02998
        1901 " Belgium"                   56.99522
        1901 " Belize"                    155.5927
        1901 " Benin"                    106.23543
        1901 " Bermuda"                  145.39166
        1901 " Bhutan"                   150.91719
        1901 " Bolivia"                   83.77482
        1901 " Bosnia & Herzegovina"  95.28339
        1901 " Botswana"                  38.90192
        1901 " Brazil"                   145.47807
        1901 " British Virgin Is."       251.01346
        1901 " Brunei"                   263.47974
        1901 " Bulgaria"                  60.55573
        1901 " Burkina Faso"             70.304115
        1901 " Burundi"                   59.11721
        1901 " Cambodia"                 153.46115
        1901 " Cameroon"                 138.90353
        1901 " Canada"                    36.75501
        1901 " Cape Verde"               13.387116
        1901 " Cayman Is."                  148.55
        1901 " Central African Republic" 113.15446
        1901 " Chad"                      27.86726
        1901 " Chile"                     60.08192
        1901 " China"                     42.71596
        1901 " Cocos Is."                173.41667
        1901 " Colombia"                 230.41254
        1901 " Comoros"                  133.40213
        1901 " Congo"                    129.84007
        1901 " Cook Is."                 178.59627
        1901 " Costa Rica"                292.5924
        1901 " Cote d'Ivoire"            115.32276
        1901 " Croatia"                   93.82109
        1901 " Cuba"                     148.61938
        1901 " Cyprus"                   22.324896
        1901 " Czech Republic"            54.21322
        1901 " Denmark"                   52.24304
        1901 " Djibouti"                  38.89595
        1901 " Dominica"                 250.29575
        1901 " Dominican Republic"       145.68788
        1901 " Ecuador"                  224.39095
        1901 " Egypt"                    4.0063434
        1901 " El Salvador"              150.94292
        1901 " Equatorial Guinea"         192.1228
        1901 " Eritrea"                  17.697659
        1901 " Estonia"                  37.476612
        1901 " Ethiopia"                  70.71775
        1901 " Faroe Is."                127.29942
        1901 " Fiji"                     239.81703
        1901 " Finland"                   31.64179
        1901 " France"                    67.05617
        1901 " French Polynesia"         175.05746
        1901 " Gabon"                    143.07632
        1901 " Georgia"                   82.04367
        1901 " Germany"                   55.73378
        1901 " Ghana"                     103.4563
        1901 " Gibraltar"                 75.31437
        1901 " Greece"                    63.62523
        1901 " Greenland"                 45.90203
        1901 " Grenada"                  127.87691
        1901 " Guam"                      640.9429
        1901 " Guatemala"                213.67705
        1901 " Guinea"                   156.88628
        1901 " Guinea-Bissau"            163.56067
        1901 " Guyana"                   180.83296
        1901 " Haiti"                    133.56944
        1901 " Honduras"                  162.6607
        1901 " Hungary"                   50.82848
        1901 " Iceland"                   76.34325
        1901 " India"                     78.03558
        1901 " Indonesia"                234.19624
        1901 " Iran"                     15.219575
        1901 " Iraq"                     14.566815
        1901 " Ireland"                   87.40076
        1901 " Israel"                   17.971825
        1901 " Italy"                     93.88945
        1901 " Jamaica"                   217.2105
        1901 " Japan"                     137.2933
        1901 " Jordan"                    8.030041
        1901 " Kazakhstan"               22.792145
        1901 " Kenya"                     53.95895
        1901 " Kiribati"                  96.95818
        1901 " Kuwait"                    5.186317
        1901 " Kyrgyzstan"                40.02524
        1901 " Laos"                     142.41025
        end

        Comment


        • #5
          Compare the results of:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int year str25 country float rainfallmm
          1901 " Afghanistan"              20.910303
          1901 " Albania"                  103.67812
          1901 " Algeria"                   9.454398
          end
          gen country2= ltrim("")
          gen country3= ltrim(" something")
          gen country4= ltrim(country)

          Comment


          • #6
            Thank you Jorrit,

            Code:
             
             gen country4= ltrim(country)
            Works perfectly!

            Best,
            Katrine

            Comment

            Working...
            X