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: How to apply a command to numeric variables only


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: How to apply a command to numeric variables only
Date   Wed, 30 May 2012 09:00:28 +0100

Note that you had

> . foreach var of varlist srhcareactivity1-srhcareactivity6{
>  2.         capture decode `var', generate(str)
>  3.         if _rc == 0 {
>  4.                 drop `var'
>  5.                 rename string `var'
>  6.         }
>  7. }

That should not have worked at all, because you create -str- and
rename -string-.

Further, note that -ds- and -findname- (SJ, SSC) both offer ways to
select variables with value labels:

ds, has(vall)

findname, vall

Nick

On Wed, May 30, 2012 at 12:57 AM, Nick Cox <[email protected]> wrote:
> Comments embedded below.
>
> On Tue, May 29, 2012 at 9:58 PM, Paul O'Brien <[email protected]> wrote:
>> These are the results:
>> . ds srhcareactivity1-srhcareactivity6 , has(type numeric)
>> srhcareact~4  srhcareact~5  srhcareact~6
>>
>> . foreach var in `r(varlist)' {
>>  2.         decode `var', generate(string)
>>  3.         drop `var'
>>  4.         rename string `var'
>>  5. }
>> srhcareactivity4 not labeled
>> r(182);
>> ===
>
> Your problem was posed that you want to -decode- variables, except
> that you can't -decode- string variables. But you can't -decode-
> variables without value labels either. So let's suppose you just want
> a string version.
>
>  foreach var in `r(varlist)' {
>         capture decode `var', generate(work)
>         if _rc gen work = string(`var')
>         drop `var'
>         rename work `var'
>  }
>
> I used the name -work- instead of -string-, if only as a matter of
> style. This could fail to work well too, but you need to tell us more
> about the data or about what you want to get better advice.
>
>> . findname srhcareactivity1-srhcareactivity6 , type(numeric)
>> srhcareact~4  srhcareact~5  srhcareact~6
>>
>> . foreach var in `r(varlist)' {
>>  2.         decode `var', generate(string)
>>  3.         drop `var'
>>  4.         rename string `var'
>>  5. }
>> srhcareactivity4 not labeled
>> r(182);
>> ===
>
> That's the same problem.
>
>> . foreach var of varlist srhcareactivity1-srhcareactivity6{
>>  2.     capture decode `var', generate(string)
>>  3.     if _rc == 0 {
>>  4.             drop `var'
>>  5.             rename string `var'
>>  6.      }
>>  7. }
>> unrecognized command:      capture invalid command name
>> r(199);
>> ===
>
> Can't see anything wrong there.
>
>> . ds srhcareactivity1-srhcareactivity6 , has(type numeric)
>> srhcareact~4  srhcareact~5  srhcareact~6
>>
>> . foreach var of varlist srhcareactivity1-srhcareactivity6{
>>  2.         capture decode `var', generate(str)
>>  3.         if _rc == 0 {
>>  4.                 drop `var'
>>  5.                 rename string `var'
>>  6.         }
>>  7. }
>>
>> but not decoded:
>> srhcareactivi~4 byte   %8.0g                  SRHCareActivity4
>> srhcareactivi~5 byte   %8.0g                  SRHCareActivity5
>> srhcareactivi~6 byte   %8.0g                  SRHCareActivity6
>
> Quite so. The signal above was that the fourth variable was not
> labelled. Therefore it can't be -decode-d. But the -capture- ate the
> error, and the variable is unchanged. Same story with 5 and 6.
>
> It seems quite odd that variables with the name can present as (1)
> string (2) numeric with value labels (3) numeric in different
> datasets. Care to comment on why this happens?
>
> Nick
>>
>>
>> Paul
>>
>> On 29 May 2012, at 11:00, Nick Cox wrote:
>>
>>> You are reporting problems but not the exact commands you tried. I
>>> could try guessing what you did wrong, and whether it is my fault or
>>> yours, but it is really is much better if you show your code.
>>>
>>> Nick
>>>
>>> On Tue, May 29, 2012 at 10:55 AM, paul o'brien <[email protected]> wrote:
>>>> Thanks NIck,
>>>>
>>>> I encounter a problem with each of these suggestions:
>>>>
>>>> With ds and findname I get: srhcareactivity4 not labeled
>>>>
>>>> With -capture- inside the loop I get: variable string not found
>>>>
>>>> On keeping the label I get: unrecognized command:      local invalid =
>>>> command name
>>>>
>>>> Can you help?
>>>>
>>>> Thanks for all your very useful advice on the List.
>>>>
>>>> Paul
>>>>
>>>>
>>>> On 29/05/2012, Paul O'Brien <[email protected]> wrote:
>>>>> Thanks NIck,
>>>>>
>>>>> I encounter a problem with each of these suggestions:
>>>>>
>>>>> With ds and findname I get: srhcareactivity4 not labeled
>>>>>
>>>>> With -capture- inside the loop is get: variable string not found
>>>>>
>>>>> On keeping the label I get: unrecognized command:      local invalid command
>>>>> name
>>>>>
>>>>> Is there a tweak we are missing?
>>>>>
>>>>> Paul
>>>>>
>>>>> On 29 May 2012, at 08:27, Nick Cox wrote:
>>>>>
>>>>>> Similar questions arise often on this list. In your case one solution
>>>>>> is to -destring- the string variables before the loop, but then the
>>>>>> loop will convert back, so that does not appeal.
>>>>>>
>>>>>> Another is to select numeric variables beforehand. -ds- will do this
>>>>>>
>>>>>> ds srhcareactivity1-srhcareactivity6 , has(type numeric)
>>>>>>
>>>>>> foreach var in `r(varlist)' {
>>>>>>
>>>>>> -findname- (SJ) can do more than -ds- (fact) and has a better syntax
>>>>>> (opinion), but its use is very similar here.
>>>>>>
>>>>>> findname srhcareactivity1-srhcareactivity6 , type(numeric)
>>>>>>
>>>>>> foreach var in `r(varlist)' {
>>>>>>
>>>>>> Another way is to use -capture- inside the loop.
>>>>>>
>>>>>> foreach var of varlist srhcareactivity1-srhcareactivity6{
>>>>>>     capture decode `var', generate(string)
>>>>>>     if _rc == 0 {
>>>>>>             drop `var'
>>>>>>             rename string `var'
>>>>>>      }
>>>>>> }
>>>>>>
>>>>>> Is it "string" a reserved word? (I am away from manuals at the
>>>>>> moment.) I'd use something different here, even if not.
>>>>>>
>>>>>> Keeping the variable label can be done with extended macro functions,
>>>>>> mentioned frequently on this list.
>>>>>>
>>>>>> foreach var of varlist srhcareactivity1-srhcareactivity6{
>>>>>>     local varlabel : variable label `var'
>>>>>>     capture decode `var', generate(string)
>>>>>>     if _rc == 0 {
>>>>>>             drop `var'
>>>>>>             rename string `var'
>>>>>>             label var `var `varlabel'
>>>>>>      }
>>>>>> }
>>>>>>
>>>>>> See -help extended fcn-. Or -help macro- first.
>>>>>>
>>>>>> Nick
>>>>>>
>>>>>> On Tue, May 29, 2012 at 6:28 AM, Paul O'Brien <[email protected]>
>>>>>> wrote:
>>>>>>> I want to do a foreach command to convert numeric variables to string
>>>>>>> before a merge. Only some of the variables are numeric and which ones
>>>>>>> varies with each database.
>>>>>>>
>>>>>>> foreach var of varlist srhcareactivity1-srhcareactivity6{
>>>>>>>        decode `var', generate(string)
>>>>>>>        drop `var'
>>>>>>>        rename string `var'
>>>>>>> }
>>>>>>>
>>>>>>> Error message: not possible with string variable.
>>>>>>>
>>>>>>> I have tried using capture to ignore the error.
>>>>>>>
>>>>>>> How can I restrict the command to numeric variables only?
>>>>>>>
>>>>>>> By the way, is there a way to keep the variable label which is lost when
>>>>>>> I use generate.
>>>>>>>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index