Statalist


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

Re: st: new string variable which has the formatted value of a variable


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: new string variable which has the formatted value of a variable
Date   Wed, 07 Jan 2009 11:52:09 -0500


You can skip a step and use the %fmt option of
-file write- to write formatted values:

 file write ... %td (jdate[`i']) ...

will produce "human readable" dates.

cheers,
Jeph




Ashim Kapoor wrote:
My apologies for my poor articulation.

Suppose I have a variable say jdate

jdate
17890
17891
17892

etc.

so I can say format %td jdate

And the dates look like " Sep 16 1990 " ,etc in a human readable form.
but they only LOOK human readable they have only been displayed like
that, the variable jdate contains the values 17890, etc.

Now if i write jdate to an HTML file whose code is actually a text
file , I would do

file write  ( .......   jdate[`i']........... )

where i helps me loop over observations and create a table in HTML.

if jdate is as it is NO MATTER how I have formatted it , my html table
won't have human readable dates, it will have 17890 / 17891 / etc

but if I do -tostring- then the variable not only DISPLAYS as human
readable in stata , it IS actually containng human readable values
and when I write to HTML it will be human readable.

I hope I was more articulate this time.

Thank you,
Ashim

On Wed, Jan 7, 2009 at 9:44 PM, Nick Cox <[email protected]> wrote:
-tostring- is objecting because -real("28feb2008")- does not yield
17590.

That objection is irrelevant for your purposes, so you should just add
the -force- option.

Do please notice that I didn't recommend using -tostring-; I recommended
using -string()- directly.

I don't follow your argument about HTML but I will not comment further
on that. It is really is impossible to discuss what you are doing when
the description of your code is merely "it".

Nick
[email protected]

Ashim Kapoor

hi Nick,

Thank you for your email.

I need it to be a string because I want it to be output to HTML. If
the orginal variables is 17590 and display something like " Sep 6
1990" it is useless as when i try writing that variable to an HTML
code it will write 17590 and not Sep 6 1990 which is what I want.

gen k=17590
. format %td k

. l

    +-----------+
    |         k |
    |-----------|
 1. | 28feb2008 |
 2. | 28feb2008 |
 3. | 28feb2008 |
 4. | 28feb2008 |
 5. | 28feb2008 |
    +-----------+

. tostring(k) ,format(%td) gen(j)
k cannot be converted reversibly; no generate

This fails. Am I doing something wrong?

Thank you,
Ashim.
On Wed, Jan 7, 2009 at 9:17 PM, Nick Cox <[email protected]> wrote:
Using -tostring- would be convenient for several variables, and it
certainly supports a -format()- option. (I am not clear why Jeph
appears
uncertain on that.)

But a lower-level route appears closer to what Ashim wants. That uses
the two-argument flavour of -string(,)-.

To detect what format a variable has you go

local fmt: format mynumvar
gen svar = string(mynumvar, "`fmt'")

Thus no magic is needed, just willingness to read the documentation.

I don't understand why a string variable is needed here. Once a date
has
been displayed in SMCL whether it came from a numeric variable or a
string variable is immaterial, but that is a separate issue.

Nick
[email protected]

Jeph Herrin

I think -tostring- will work with the -format()- option.
Or, if you want to call mata you can use -sprintf()-.


Ashim Kapoor wrote:

This is great. Will do part of what I what . I still need the magic
command to do magic on dates.
On Wed, Jan 7, 2009 at 5:21 PM, Neil Shephard <[email protected]>
wrote:
Not sure about dates, but for your second example you can do the
following....
gen str magicj = string(round(j, .01))

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

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

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index