Announcement

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

  • Space in concat function

    Hello,
    I am trying to combine 10 string variables in one. I want space after each variable entry in the new variable. Some of these variables have no values.

    When I use the Concat function, then there is no space between the resulting variable entries(see variable crop4). I also tried using catenate command (see variable crop_harvestmain); however, it leaves space for each variable where there is no value. So, if I have values in var 1 var3 var7, catenate gives me "1 3 7", concat gives me "137'; whereas I want "1 3 7" irrespective of which value is missing.
    Please help.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1(crop_harvest1 crop_harvest2 crop_harvest3) str18 crop_harvestmain str8 crop4
    "1" ""  "3" "1  3 4 5 6 7   "  "134567"
    ""  ""  "3" "  3 4  6    "     "346"    
    "1" ""  "3" "1  3 4 5 6    "   "13456"  
    "1" ""  ""  "1   4 5 6 7   "   "14567"  
    "1" ""  ""  "1    5 6    "     "156"    
    "1" ""  ""  "1    5 6    "     "156"    
    "1" ""  "3" "1  3 4 5 6 7   "  "134567"
    "1" ""  "3" "1  3  5 6 7   "   "13567"  
    "1" ""  "3" "1  3  5 6 7   "   "13567"  
    "1" ""  "3" "1  3   6    "     "136"    
    "1" ""  "3" "1  3 4 5 6    "   "13456"  
    "1" ""  "3" "1  3 4 5 6 7   "  "134567"
    "1" ""  "3" "1  3  5 6 7   "   "13567"  
    "1" ""  "3" "1  3 4 5 6 7   "  "134567"
    "1" ""  "3" "1  3  5 6 7   "   "13567"  
    "1" ""  "3" "1  3  5 6 7   "   "13567"  
    "1" "2" "3" "1 2 3  5 6 7   "  "123567"
    "1" ""  ""  "1    5 6    "     "156"    
    
    end
    Last edited by Saini Smriti; 15 Jul 2020, 07:16.

  • #2
    This should help by getting rid of extra spaces between digits, as well as any leading or trailing spaces.
    Code:
    replace crop_harvestmain = trim(stritrim(crop_harvestmain))

    Comment


    • #3
      William Lisowski gives excellent advice. It's worth pointing out too that egen, concat() isn't rocket science; it just is a loop over the variables supplied, with some bells and whistles added. So, if you want different behaviour, write your own loop.

      Presumably the original data in full looked like this:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str1(crop_harvest1 crop_harvest2 crop_harvest3 crop_harvest4 crop_harvest5 crop_harvest6 crop_harvest7)
      "1" ""  "3" "4" "5" "6" "7"
      ""  ""  "3" "4" ""  "6" ""
      "1" ""  "3" "4" "5" "6" ""
      "1" ""  ""  "4" "5" "6" "7"
      "1" ""  ""  ""  "5" "6" ""
      "1" ""  ""  ""  "5" "6" ""
      "1" ""  "3" "4" "5" "6" "7"
      "1" ""  "3" ""  "5" "6" "7"
      "1" ""  "3" ""  "5" "6" "7"
      "1" ""  "3" ""  ""  "6" ""
      "1" ""  "3" "4" "5" "6" ""
      "1" ""  "3" "4" "5" "6" "7"
      "1" ""  "3" ""  "5" "6" "7"
      "1" ""  "3" "4" "5" "6" "7"
      "1" ""  "3" ""  "5" "6" "7"
      "1" ""  "3" ""  "5" "6" "7"
      "1" "2" "3" ""  "5" "6" "7"
      "1" ""  ""  ""  "5" "6" ""
      end
      Then an alternative loop could be

      Code:
      gen wanted = ""
      
      forval j = 1/7 {
           replace wanted = wanted + crop_harvest`j' + " " if crop_harvest`j' != ""
      }
      
      replace wanted = trim(wanted)
      
      
      l wanted
      
           +-------------+
           |      wanted |
           |-------------|
        1. | 1 3 4 5 6 7 |
        2. |       3 4 6 |
        3. |   1 3 4 5 6 |
        4. |   1 4 5 6 7 |
        5. |       1 5 6 |
           |-------------|
        6. |       1 5 6 |
        7. | 1 3 4 5 6 7 |
        8. |   1 3 5 6 7 |
        9. |   1 3 5 6 7 |
       10. |       1 3 6 |
           |-------------|
       11. |   1 3 4 5 6 |
       12. | 1 3 4 5 6 7 |
       13. |   1 3 5 6 7 |
       14. | 1 3 4 5 6 7 |
       15. |   1 3 5 6 7 |
           |-------------|
       16. |   1 3 5 6 7 |
       17. | 1 2 3 5 6 7 |
       18. |       1 5 6 |
           +-------------+

      Comment


      • #4
        Thanks, William and Nick.
        I am learning how to run loops. Nick, the code you provided helps me in advancing my use of loops. Thanks, again!

        Comment

        Working...
        X