Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re:st: Extracting numbers from code

From   daniel klein <>
Subject   Re:st: Extracting numbers from code
Date   Mon, 12 Nov 2012 18:21:04 +0100


it is very important to reply to the list, not write its members
private messages. One reason for this is, that what you do not
undestand, others might also find hard to understand, and they will
benefit from an public answer. This motivates me more to put extra
effort into an explanation of my code. Second, you get a chance others
look at my code or modifications, too. In this case this is crucial,
because my code still does not do the job!

I will start with an explanation of what the code does.

The first logical expression

substr(strofreal(item), 1, 1) == "1"

checks whether the first character in <item> is a "1" or not. It
returns "true" (1) if it is, "false" (0) if not. See -help string
functions- for this. The problem is, that there might be respondents
who checked items 10 or 11 only. These respondents will be coded as if
the had checked item 1, even though this is not the case. The line

!(inlist(substr(strofreal(item), 2, 1), "0", "1"))

therefore checks, whether the second character in <item> correspoonds
to a "0" (as is the case for 10) or "1" (as is the case for 11). See
-help inlist()- for this. The idea is/was not falsly to code those who
checked either 10 or 11 as if they had checked item 1.

However, as should be evident, this code does not work either. For
those who did not only check item 1, but also item 10, or 11, or both,
the variable <item> will take the values 110, 111, or 11011. With the
above logic, neither of these cases will be coded correctly, because
the second character in <item> is a "0" or a "1" in any case. Now, I
have an ugly work-around for this. It will (probably) work, but it is
an ad-hoc fix, and not very general. Try

g byte dummy1 = (substr(strofreal(item), 1, 1) == "1") & ///
(cond((strlen(strofreal(item)) < 3), ///
cond(!(inlist(substr(strofreal(item), 2, 1)), "0", "1"), 1, 0), 1))

See -help cond()- for this.

Hi Daniel.
I just wanted to thank you very much for the answers you just sent me
on the statalist! They work great!!!! This will save me SO much time.
If you have a second, could you explain what the second line of code
does? If not, no problem
!(inlist(substr(strofreal(item), 2, 1), "0", "1"))
Thank you again!
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index