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
Thu, 6 Feb 2014 22:42:35 +0000
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.
Nick
[email protected]
On 6 February 2014 21:34, Tim Evans <[email protected]> wrote:
> Hi,
>
> 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.
>
> Any help much appreciated
>
> Best wishes
>
> Tim
>
>
> **************************************************************************
> The information contained in the EMail and any attachments is confidential and intended solely and for the attention and use of the named addressee(s). It may not be disclosed to any other person without the express authority of Public Health England, or the intended recipient, or both. If you are not the intended recipient, you must not disclose, copy, distribute or retain this message or any part of it. This footnote also confirms that this EMail has been swept for computer viruses by Symantec.Cloud, but please re-sweep any attachments before opening or saving. http://www.gov.uk/PHE
> **************************************************************************
>
> *
> * 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/
*
* 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/