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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: For Loop Using Variable Labels


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: For Loop Using Variable Labels
Date   Mon, 24 Jun 2013 19:10:13 -0400

clear
sysuse voter
local v "inc"

local vl :value label `v'
di `"`vl'"'

tempfile tmp
label save `vl' using `tmp'

tempfile tmp2
filefilter "`tmp'" "`tmp2'", from(", modify") to("")
filefilter "`tmp2'" "`tmp'", from("label define `vl' ") to("") replace

preserve
insheet using "`tmp'", delim(" ") clear
levelsof v1, local(universe)
restore

foreach lev in `universe' {
  summarize `v' if `v'==`lev'
}

On Mon, Jun 24, 2013 at 7:04 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> OK. Then I guess you should look at
>
> help extended_fcn
>
> to see how to look up value labels given values.
> Nick
> njcoxstata@gmail.com
>
>
> On 24 June 2013 23:53, Erika Kociolek <ekociole@gmail.com> wrote:
>> Yes, I do mean value label. Apologies for confusing terminology.
>>
>> On Mon, Jun 24, 2013 at 3:49 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>> You say
>>>
>>> "I'd like to use
>>> the "levelsof" command on the variable label instead of the variable
>>> values, since the label for the lowest category will always be in the
>>> dataset, but there may not be a variable value for each response
>>> category"
>>>
>>> When you say _variable label_ do you mean _value label_ ? .
>>> Nick
>>> njcoxstata@gmail.com
>>>
>>>
>>> On 24 June 2013 23:40, Erika Kociolek <ekociole@gmail.com> wrote:
>>>> Let's say I wanted to write to a file the number of responses to a
>>>> variety of survey questions with different categories. I'll use the
>>>> voter dataset to illustrate what I want to do.
>>>>
>>>> **********************
>>>> *Stata Code*
>>>>
>>>> cd C:/PATH/Data/
>>>> sysuse voter, clear
>>>> keep candidat-inc
>>>> drop if inc == 1  /*Drop if inc variable is equal to 1 (< $15k)*/
>>>>
>>>> file open mefile using ../Data/Test.txt, write replace
>>>> file write mefile "Variable" _tab "Value" _tab "Number" _tab "Total" _tab _n
>>>>
>>>> foreach v of varlist candidat-inc{ /*For each variable in the dataset:*/
>>>>    levelsof `v', local(temp) /*Store variable values in a local macros.*/
>>>>
>>>>    foreach t of local temp{ /*For each value of the variable:*/
>>>>       file write mefile "`v'" _tab /*Write variable name to file,*/
>>>>       file write mefile "`t'" _tab /*Write question response to file,*/
>>>>       summarize `v' if `v' == `t'
>>>>       file write mefile "`r(N)'" _tab /*Write value to file,*/
>>>>    }
>>>>    summarize `v'
>>>>    file write mefile "`r(N)'" _tab _n /*Write total number of
>>>> responses to the question (variable) to file.*/
>>>> }
>>>>
>>>> file close mefile
>>>> **********************
>>>>
>>>> So I dropped the lowest income category from the dataset (let's assume
>>>> there were no responses in that particular category). I'd like to use
>>>> the "levelsof" command on the variable label instead of the variable
>>>> values, since the label for the lowest category will always be in the
>>>> dataset, but there may not be a variable value for each response
>>>> category. How would one do that?
>>>>
>>>> Thanks!
>>>> *
>>>> *   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/
>> *
>> *   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/
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index