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   Tim Evans <[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:38:37 +0000

Nick,

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?

Best wishes

Tim

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Tim Evans
Sent: 07 February 2014 10:53
To: [email protected]
Subject: RE: st: Insert spaces into a string variable upto the max length of a variable.

Nick,

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

Best wishes

Tim

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 06 February 2014 22:43
To: [email protected]
Subject: Re: st: Insert spaces into a string variable upto the max length of a variable.

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/

**************************************************************************
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/

**************************************************************************
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/


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