Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Iterating through value label content

    Hi everybody,

    I am sure I am not the first person asking this, but after wasting an hour trying to replicate solutions on the internet, I am sick of trial and error (more error than trial). My question is as follows:

    In one survey, the subjects had to answer the question which kind of activity they spend their most money on. Every subject could specify up to three types of spending, therefore I have three spending variables for each observation/subject (spending1, spending2, spending3). As there were 9 possible kind of acitivities to choose from, I created a value label SPEND with 9 different values (1 to 9). For example, the value 1 represents "housing", 2 represents "transportation" and 3 represents "multimedia and electronical devices".

    I now want to calculate which kind of activity was mentioned the most (no matter if it was mentioned in spending1, spending2 or spending3). Thats what I got so far:

    Code:
    quietly label list SPEND
    scalar uselabelnames = r(names)
    
    forvalues i = `r(min)'/`r(k)'{
        gen n_spend_`i' = (spending1==`i') + (spending2==`i') + (spending3==`i')
        label var n_use_`i' uselabelnames(`i')  (******PROBLEM****)
     }
    
    estpost tabstat n_spend_*, stats(mean sd min max) columns(statistic)
    estimates store wayofspending
    This programm is working (except the line with Problem). However, I know want to export the results into a csv file. I therefore use
    Code:
    esttab wayofusing using source.csv, replace main(mean) aux(sd) label ///
        b(%9.3f) stats(N, fmt(%9.0g)) ///
        title ("Ways of spending money")
    The idea was to link the existing labels of SPEND with the new created variables, so that the variable n_use_1 has the label/content of SPEND given value 1, n_use_2 has the label/content of SPEND given value 2 and so forth. If that would be the case, I could simply use the label command in esttab and see the labels of the variables, instead of the names of the variables in my csv file. However, I don't know how to implement that kind of idea, as Scalars are appearently the wrong idea (I'm not even sure if I can get access to a scalar in that kind of way).

    I also tried to use something like "legend coeflabels(uselabelnames)" in the esttab command, but it did not work either.

    In other programming languages, I would do the following: Save the content (not the integer values, but the strings!) of SPEND into a string array (it has got 9 array fields). Inside the for loop, I would iterate through the string array, so that variable n_use_i gets the label that is on position i in the string. However, I have absolutely no idea how to do that in Stata. I would be glad if anybody could help me.
    Thank you very much in advance!


  • #2
    Here are some thoughts:
    1) I don't commonly use -label list-. When I looked at the documentation for it (-help label list-), and at the results of trying your -label list- command, I see no indication that it returns r(names). I'm not sure what led you to think this would work. You will need some of the label commands documented in -help extended_fcn- to assemble a list of labels associated with SPEND.
    2) I think you'll be much better off using a local rather than a scalar to hold text material. Even if
    -scalar uselabelnames = r(names)- worked, it would be undesirable, as locals with text are much easier to program with than string scalars. In fact, I have never had occasion to use a string scalar in Stata, and have never seen anyone else use them for this purpose.
    3) Your use of -uselabelnames(`i')- is not legal Stata code that I recognize. I gather that you want to extract part of -uselabelnames- as thought it were a vector of some sort, and it appears you are using a syntax construction drawn from some other language in which something analogous to uselabelnames(`i') is legal.

    Instead, I'd suggest you put your labels into locals with names that identify the label you want. So, you might have a local such as vlabel1 = "housing" and so forth. (Where you might think "array of strings" in another language, think "locals with a number in their name" in Stata. At least that's what I like to do.) Again, the relevant programming features are documented tersely in -help extended_fcn-. Something like the following is likely what you want:
    Code:
    levelsof spending1, local(vlist)  // I'm assuming your variable spending1 has all possible values
    foreach v of local vlist {
       local vlabel`v': label SPEND `v'
    }
    // display the results
    foreach v of local vlist {
       di "label for value `v' = `vlabel`v''"
    }
    //
    4) You reference var n_use_`i' but have not described it to us, so perhaps it is an existing variable, or perhaps not, in the later case of which there is a problem.

    Now, for your main event, getting the material you want into a CSV file. Perhaps others here can decipher the desired form of your CSV file, but I'm not understanding you. An simple example here is likely to be more helpful to us than a verbal description.
    Last edited by Mike Lacy; 08 Apr 2018, 10:15.

    Comment


    • #3
      Thank you for your help and sorry for the late response. Basically, I want to create a CSV or RTF file to have a brief overview of parts of the data outside Stata.

      I create 9 variables named n_spend_1, n_spend_2 asf in order to know how often each of the nine categories was mentioned in the questionnaire (no matter if it was the most important, second most or third most spending type. After creating the 9 new variables (each of them only has the value 0 or 1 for each observation), I generate another 9 variables with egen count to get the absolute number for each of the nine categories by counting all observations where the value is 1 (however, that is missing here, so let's assume n_spend_* already shows how often spending type * was mentioned).

      Till now, I have to write each kind of spending by hand:

      title ("Ways of spending additional income") legend coeflabels(n_spend_1 "Everyday consumption of family" n_spend_2 "Healthcare expenses of family" n_spend_3 "Education of children"

      However, it would be much easier if I could just write something like title("Ways of spending additional income") legend coeflabels(n_spend_* SPEND) so that I do not have to write every label myself (and, if labels change in SPEND, they change automatically in the RTF file). That's why I wanted to store the labels, so that I do not have to look up the value labels myself and write their names down. I hope it is clearer now. Is that possible in Stata?

      Comment


      • #4
        I'm sorry, but I can't understand very much of what you want. The fact that others have not responded suggests to me that I'm not the only one confused.

        I'd suggest you try to show the text of your question to a friend--not necessarily a Stata user but numerate--as a way to help you figure out how to present it in a way that would be more accessible to other people. And, per the standard instructions in the FAQ (top left of your screen), presenting an example of your data and and what you want in the way of output is almost always the most effective way to help people help you.

        Comment


        • #5
          Okay, give me a last and final try (I have a different idea that may be easier to understand):

          I have a categorical variable named "spend". The variable has (for simplicity) three different values. 1 stands for "Food consumption", 2 for "Medical expenses" 3 for "Rent" (that is where the value label SPENDING comes in place). I now create three variables named food, medical, rent with are one if the variable spend has the value 1, 2 or 3 respectively (if somebody said their spending is for food consumption, the variable food has the value one and the variables medical and rent have value zero). I now want to label the three variables according to the value in the value label SPENDING, so that variable food gets the label Food consumption, medical gets the label Medical expenses and so on. Of course I could write label variable food "Food consumption" on my own, but I would prefer that this assignment happens automatically (so that, in case the descriptions in the value label change, for example value 1 stands now for "Food consumption and theater", the label of the variable food also changes automatically to "Food consumption and theater").

          In order to make it easier (concerning loops), I have had the idea to name the new variables according to the values they correspond to in the value label (so food is named spend1, medical is renamed to spend2, rent renamed to spend3).
          Basically, I just want that the variable named spend`i' gets the corresponding value label `i' of SPENDING assigned as the variable's label.

          I hope that makes it easier. It is just a general question if that is possible in Stata or not.

          Comment


          • #6
            Mike asked twice for a data example to no avail. That means an example, not a description. But this description allows engineering works.

            Code:
            clear 
            input spend 
            1
            2
            3
            end 
            
            label def spend 1 "Food consumption" 2 "Medical expenses" 3 "Rent" 
            
            local newnames food medical rent 
            forval j = 1/3 { 
                gettoken this newnames : newnames 
                gen `this' = spend == `j' 
                label var `this' "`: label spend `j''" 
            }
            
            describe
            
            Contains data
              obs:             3                          
             vars:             4                          
             size:            48                          
            ---------------------------------------------------------------------------------
                          storage   display    value
            variable name   type    format     label      variable label
            ---------------------------------------------------------------------------------
            spend           float   %9.0g                 
            food            float   %9.0g                 Food consumption
            medical         float   %9.0g                 Medical expenses
            rent            float   %9.0g                 Rent
            ---------------------------------------------------------------------------------
            Sorted by: 
                 Note: Dataset has changed since last saved.
            So, there is an easy bit here. To copy value labels to variable labels, loop over the variables and look up the value labels you need.

            Code:
            help extended fcn
            documents the looking up of value labels.

            The last bit -- wanting a value label change automatically to be echoed to a variable label change -- is I think programmable, but not trivial. Value labels are not in themselves linked to variable labels, or vice versa. That is, you can only get it, I think, if you changed your value labels via a specially written command that automatically checked whether the same text was in use. as a variable label. You can get much of the way there by using ds or findname (Stata Journal) or lookfor to find the variables in question.

            For yet another take on a similar question, see dummieslab from SSC.

            Comment

            Working...
            X