Announcement

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

  • Alignment of Values of String variables

    Good morning All
    Please I have always had a problem importing from excel, I have read up the blogs and seem not to find a solution. I would appreciate some guidance
    Below is my import command and the output. The values of the string variables come as shown, different alignments and ultimately each represent different values such that a simple tab, as below gives different counts for each alignment of the values.
    Kindly guide on how to overcome that.
    Thank you
    Ezeanosike Obumneme

    import excel "C:\Users\obumn\Desktop\HAND HYGIENE\Expoort Hand hygiene.xlsx", sheet("Sheet1") firstrow clear


    [IMG]file:///C:/Users/obumn/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg[/IMG]



    [IMG]file:///C:/Users/obumn/AppData/Local/Temp/msohtmlclip1/01/clip_image004.png[/IMG]

    [IMG]file:///C:/Users/obumn/AppData/Local/Temp/msohtmlclip1/01/clip_image006.png[/IMG]

  • #2

    #1
    Alignment of Values of String variables

    Please I had difficulty copying and pasting the output Alignment of String Variables.docx

    Comment


    • #3
      Apparently the problem is that the data in your Excel worksheet has extra leading and trailing blank characters. The example below reproduces your problem, creating an Excel worksheet with extra blank characters in a string variable. Then it demonstrates using the trim function to resolve the problem.
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD20026.000000"
      
      . * Example generated by -dataex-. To install: ssc install dataex
      . clear
      
      . input str3 v
      
                   v
        1. "a"  
        2. "a " 
        3. " a "
        4. end
      
      . export excel test, firstrow(variables) replace
      file test.xls saved
      
      . clear
      
      . import excel test, firstrow
      
      . tab v
      
                v |      Freq.     Percent        Cum.
      ------------+-----------------------------------
               a  |          1       33.33       33.33
                a |          1       33.33       66.67
               a  |          1       33.33      100.00
      ------------+-----------------------------------
            Total |          3      100.00
      
      . replace v = trim(v)
      (2 real changes made)
      
      . tab v
      
                v |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                a |          3      100.00      100.00
      ------------+-----------------------------------
            Total |          3      100.00
      
      .

      Comment


      • #4
        Thank you so much William
        It worked perfectly


        Comment

        Working...
        X