| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Padding zeros, writing functions
The short answers are No and No, but the long answer is that
the solution is there already for use.
0. Writing functions: You can't do this. You can write
commands via ado files, -egen- functions, Mata functions, but not
Stata functions. Thus there is no documentation on how to do this.
Perhaps there should be documentation explaining that you can't do
it, but StataCorp tend not to document what you can't do, and
I think that's good practice.
1. If you have numbers and you want to display them with
leading zeros, that is a -format- issue and requires
no extra functions, either yours or official Stata's.
2. If you have numbers and you want to store them with
leading zeros, then
(a) this is a misunderstanding because
what you really want is a -format- (see 1 again)
or
(b) you want a string, really, in which case -tostring- is one
way to do it. -string(<number>, "<format>")- is another way
to do it and often more direct than -tostring-, and I suspect what
you most want. For example
string(15, "%03.0f")
yields
"015"
as requested. And it works fine on variables too. So, you
don't need a zero padding option because -string()- will
do it for you.
It may look as if there is some redundancy: why -tostring-
as well as -string()-? There are several answers here,
including the fact that -tostring- only applies to variables
and you must have a -string()- function for individual numbers
too. In fact, the -string()- function came long before -tostring-
and the latter is just a glorified wrapper for the former, but
with some convenience and safety features too.
Nick
[email protected]
Ari Friedman
It occurred to me today that the way that I pad numbers out to a certain
number of digits is a bit annoying. Generally, I -tostring- the variable,
then generate a new variable with 0000's in front, then -substr- from with a
negative starting position. This works fine. However, I'd like to write a
pad() function that handles things without the fuss.
But then I realized that I don't know how to write functions. ADO files,
sure. But things like the built-in functions substr(), string(), etc. I
have no clue how to program.
Ideally I want to have a function that can be used like:
- gen county_code = pad(county_number,3,"0") -
Where 3 is the number of digits to pad to (e.g. 1 becomes 001, 15 becomes
015, and 387 stays 387), and "0" is the digit or character to do the padding
with (default to zero).
It's not too difficult to do this manually, but it'd be nice to have a
simple function. The closest to a one-liner I've come is:
replace state = "0" + regexs(0) if regexm(state, "^[0-9]$")
But that's a bit bulkier than I'd like.
So my questions are these:
1) Is there a good padding function already written?
2) Can someone give me a pointer to the part of the Stata documentation that
explains how to write functions (as opposed to ADO commands)? I've looked
several times, but I have been known to miss obvious things before....
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/