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: Tostring Question


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Tostring Question
Date   Mon, 16 Dec 2013 14:23:22 -0500

Ericka,
follow Nick's advice, but I would like to comment on other things:
1) I'd rather use positive logic:
instead of coding:
capture confirm numeric variable X
if !_rc {
  ...tostring...
}

I'd code:

capture confirm string variable X
if _rc {
  ...tostring...
}
With that you declare that you want the X to be string. And then the
tostring statement inside looks logical. There is no other side effect
-- just readability.

2) Depending on what is the source of the data, you may be able to
specify the type of variable directly, to override the guessing by
Stata. This is possible for example with insheet and import excel
(stringcols() option).

Best, Sergiy Radyakin


clear

input x
.
2
4
9
.
-2
end

capture confirm string variable x

if _rc {
  display "working"
  tempvar xstr
  generate `xstr' = string(x) if !missing(x)
  drop x
  rename `xstr' x
}

list

On Mon, Dec 16, 2013 at 12:02 PM, Erika Kociolek <[email protected]> wrote:
> Hi there,
>
> I am trying to convert a byte variable (byte_var) to a string so it
> can be concatenated with other variables. Sometimes this variable is a
> string when imported into Stata, and sometimes it is not (thus the
> "capture confirm" code). When I do this using the tostring command,
> for missing values of byte_var, I get "." instead of "" (missing).
>
> Here is the code I'm using:
>
> capture confirm numeric variable byte_var
>
> if !_rc {
> tostring byte_var, force replace
> }
>
> Any suggestions or assistance would be very helpful.
>
> Best,
> Erika
> *
> *   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/


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