Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: removing leading blanks


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: removing leading blanks
Date   Sun, 1 Jan 2012 13:36:06 +0000

As the help for -charlist- explains,

" charlist may, for example, reveal the presence of problematic characters
    in a string variable which "should be" numeric.  It leaves behind a
    terse character list in r(chars); a space-separated character list in
    r(sepchars); and a space-separated numlist of ASCII codes in r(ascii).
    Any may be used in a subsequent command. Sometimes copying and pasting
    all or part of the displayed output to the command window may be the
    most practical way to use the output, say as argument to destring,
    ignore().

    Note in particular that many awkward characters may not print
    comprehensibly or visibly in your Results window. To some extent, this
    will depend on the font you choose. A particular pitfall is that
    char(32) and char(160) appear identical and may be overlooked in any
    case. The returned results showing ASCII codes are needed to solve many
    of these difficulties."

So, my guess is that -charlist- did not help you because you did not
look at its returned results, which are needed to distinguish char(9),
char(32) and char(160) -- or whatever char(#) looks like char(32) but
is not identical.

It's thus not correct that -charlist- cannot distinguish non-blank
characters. That is much of its rationale.

Nick

On Sun, Jan 1, 2012 at 10:26 AM, Abhimanyu Arora
<abhimanyu.arora1987@gmail.com> wrote:
> Yes, I had to ultimately give up after spending hours thinking about
> the anomalous (buggy) nature of -ltrim- (even -charlist- did not pick
> out a non-blank character). -substr- solved the problem luckily
>
> Apparently there was a similar situation 5 years ago on the list
> http://www.stata.com/statalist/archive/2006-09/msg00269.html (but I am
> sure the spaces were leading in my case but just to be sure I tried
> both rtrim & trim with no changes)
>
>
> Best regards
> Abhimanyu
>
>
>
> On Sun, Jan 1, 2012 at 2:54 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>> OK, but in that case why did -ltrim()- fail to do what you wanted,
>> which is where you started?
>>
>> Nick
>>
>> On Sun, Jan 1, 2012 at 3:47 AM, Abhimanyu Arora
>> <abhimanyu.arora1987@gmail.com> wrote:
>>> Thanks Nick, but doesn't it imply that the blanks are actually blanks
>>> and not any other string characters?
>>>
>>> But anyway, I was able to solve the issue by using
>>> replace var1=substr(var1,5,.)
>>>
>>> Wish you and all members of the statalist a very happy new year!
>>>
>>> Best regards
>>> Abhimanyu
>>>
>>> On Sat, Dec 31, 2011 at 7:56 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>> I find the elements of your report difficult to reconcile. You imply
>>>> that -charlist- gives no output, which is not the case.
>>>>
>>>> Nick
>>>>
>>>> On Sat, Dec 31, 2011 at 1:42 PM, Abhimanyu Arora
>>>> <abhimanyu.arora1987@gmail.com> wrote:
>>>>> Hi Nick
>>>>> Thanks for the tip.
>>>>> I followed your suggestions but somehow still am unable to get things right.
>>>>> Here are the steps I followed together with the displayed output.
>>>>> Charlist rules out non-blank spaces, but-subinstr- with char(9) or "
>>>>> ", both do not yield the desired outcome.
>>>>>
>>>>> . count
>>>>>   10
>>>>>
>>>>> . gen problem=substr(var1,1,4)
>>>>>
>>>>> . charlist problem
>>>>>
>>>>>
>>>>> . replace var1 = subinstr(var1, char(9), "", .)
>>>>> (0 real changes made)
>>>>>
>>>>>
>>>>> Now,
>>>>>
>>>>> . replace var1 = subinstr(var1, " ", "",. )
>>>>> (10 real changes made)
>>>>>
>>>>> . gen problem2=substr(var1,1,3) *It seems the 10 changes are basically
>>>>> left trimming by 1 leading blank space*
>>>>>
>>>>> . charlist problem2
>>>>>
>>>>>
>>>>> . replace var1 = subinstr(var1, " ", "",. ) *Similar recursive steps
>>>>> as above does not seem to work out*
>>>>> (0 real changes made)
>>>>>
>>>>> Cheers
>>>>> Abhimanyu
>>>>>
>>>>>
>>>>>
>>>>> On Sat, Dec 31, 2011 at 5:34 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>> What you describe as blanks may be some other character(s).
>>>>>>
>>>>>> -charlist- (SSC) is a utility which may help.
>>>>>>
>>>>>> gen problem = substr(var1, 1, 4)
>>>>>>
>>>>>> charlist problem
>>>>>>
>>>>>> After finding out which characters are present, use code such as
>>>>>>
>>>>>> replace var1 = subinstr(var1, char(9), "", .)
>>>>>>
>>>>>> where 9 is a placeholder for the argument you need. (char(9) is a tab,
>>>>>> and the characters might just be a tab.)
>>>>>>
>>>>>> Nick
>>>>>>
>>>>>> On Sat, Dec 31, 2011 at 11:54 AM, Abhimanyu Arora
>>>>>> <abhimanyu.arora1987@gmail.com> wrote:
>>>>>>
>>>>>>> All values of a string variable, var1, have 4 blank spaces in the beginning.
>>>>>>> However, when I type in the command
>>>>>>> -replace var1=ltrim(var1)- ,
>>>>>>>  the output displays "0 real changes made" (and needless to say,
>>>>>>> things remain as they were to start with)
>>>>>>> Would be really great if this puzzling issue is sorted out.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index