Search code examples
stata

Create a new variable using observations and labels


I have a variable that looks like this: enter image description here

I want a new variable that multiplies the labels with the frequency, so for example the first row would be 170,105=70,105, and 2 would be 236,377=72754 and so on. I want my new variable to look like this:

enter image description here

​​​​​​​How can I do this?


Solution

  • On the face of it you have at least 119167 observations. The "at least" refers to the possibility of missing values, not tabulated by default.

    You don't say whether you want these values in the same observations or in a much reduced new dataset. If the former, then consider this (noting that 3845 * 4 = 15380).

    clear 
    input apple freq
    1 70105 
    2 36377
    3 8840
    4 3845
    end 
    
    expand freq 
    
    tab apple 
    
    bysort apple : gen new = apple * _N
    
    tabdisp apple, c(new)
    
    ----------------------
        apple |        new
    ----------+-----------
            1 |      70105
            2 |      72754
            3 |      26520
            4 |      15380
    ----------------------
    ```