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: Insert spaces into a string variable upto the max length of a variable.


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Insert spaces into a string variable upto the max length of a variable.
Date   Fri, 7 Feb 2014 14:53:54 +0000

I have no difficulty in using string variables in -graph bar- or
-graph hbar-, meaning e.g.

sysuse auto, clear
decode foreign, gen(origin)
graph hbar (mean) mpg, over(origin)
replace origin = origin + "    " if origin == "Foreign"
graph hbar (mean) mpg, over(origin)

This example shows that right alignment can be approximated, if not
gained exactly, at least with the settings on Stata 13.1 for Windows.

Your problem arises with -twoway bar-, so please either try an
equivalent command in -graph bar|hbar|dot- or tell us _exactly_ why
that won't work in your case. As requested in the FAQ, showing us
exact syntax used helps greatly.

Nick
[email protected]

On 7 February 2014 14:38, Tim Evans <[email protected]> wrote:

> Thank you very much for your help.
>
> I have used this code in order to create a variable that is based upon a number of other variables and I wanted to ensure correct spacing between the variables when I concatenated them together. I now left align the string variable and I notice that the combined variables now align for all records.
>
> My next thing is that I want to use this variable in a -tw- bar graph. I understand that strings cannot be used in a bar graph, so I encode my newly created string variable. However, I lose the left alignment I previously enjoyed with the string variable and while the contents are the same the alignment is now right aligned (I have tried left aligning in the variable properties). Is this because what is being aligned is the numeric value behind the label? If so, how can I duplicate the exact alignment in the string variable into the numeric labelled variable?

Tim Evans

> Thanks for this - much appreciated. Am working through this with other variables now.

Nick Cox

> gen length = length(trim(strvar))
> su length, meanonly
>
> local max = r(max)
> local pad : di _dup(`max') " "
>
> replace strvar = trim(strvar) + substr("`pad'", 1, `max' - length)
>
> NB _not_ length("strvar") !!!
>
> The -length()- function finds the length. -summarize- returns the maximum length. The biggest possible "pad" is that many spaces. The length of the part of the pad needed is the difference between the length of the trimmed string and the maximum length.

On 6 February 2014 21:34, Tim Evans <[email protected]> wrote:

>> I'm using Stata 11.2. I have a string variable called 'make' containing the following:
>>
>> Buick
>> AMC
>> Lincoln
>>
>> The max string length is 7 (for Lincoln) with the smallest being 3 for AMC.
>>
>> What I want to do is assess the maximum length of the variable 'make' and then insert extra spaces in order to make the length of each value up to the max. I.e add to the end of Buick two spaces and AMC (4 spaces) and Lincoln (no spaces).
>>
>> I need this for when I concatenate the string variable with another string so that the start of the second string component is aligned for all values.

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


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