Announcement

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

  • Summing Across Coded Variables

    Hi,

    I am using stata 13.

    I have a Var1a which is coded with values from 1 to 5. Var1b is also coded with values from 1 to 5. Each value has a frequency. I am trying to count the total number of observations across Var1a and Var1b to get a total frequency figure for each label.

    For example:
    Var1a Var1b
    1 50 1 20
    2 40 2 30
    3 20 3 10
    4 10 4 25
    5 5 5 35

    I would like to create a variable Var2 which has the freq values 1:70, 2: 70 etc.

    I have been looking at the egen functions but I can't seem to find a command that would do the trick.

    Thanks,

  • #2
    What are you showing us here? It clearly isn't your data because you are showing 4 variables but only give two variable names? Is this some sort of tabulation? If so, that doesn't help. What do the data look like? If you install the -dataex- command (-ssc install dataex-) and use it (-help dataex-) to post a small sample of your data, I'm confident somebody can help you.

    Comment


    • #3
      Thanks Clyde - sorry for the terrible post.

      Unfortunately, I am unable to install -dataex- so I will have to explain my data a bit better. My data looks like:
      Var1a Var1b
      1 4
      4 3
      4 2
      5 2
      1 4
      3 5
      2 4
      2 1
      Each number from 1 to 5 is labeled. For example, 1 is Laptop, 2 is PC and so forth.

      I am trying to create a new variable which would count the number of times each label (ie 1, 2, 3, 4 and 5) is recorded in Var1a and Var1b.

      I hope this is clearer. Apologies again for the bad post.

      Comment


      • #4
        Well, take a look at this:

        Code:
        clear
        input Var1a Var1b
        1 4
        4 3
        4 2
        5 2
        1 4
        3 5
        2 4
        2 1
        end
        
        tempfile holding
        save `holding'
        
        stack Var1a Var1b, into(Var2)
        contract Var2, freq(N)
        merge 1:1 _n using `holding'
        I am really not comfortable with putting Var2 and N back into the original data set with Var1a and Var1b, because there is no rhyme or reason to the pairing of the Var2/N observations with the Var1* observations. I think this kind of thing is "spreadsheet think" it is best avoided when working with Stata. But there it is.

        I don't understand what you mean when you say you can't install -dataex-. If your installation is under the control of a systems administrator and you do not have write privileges for the ado directories, ask them to do it for you. You've been posting fairly frequently here since you joined, and sooner or later, you're going to have a problem that can't be solved without the ability to replicate your data. -dataex- will be very valuable to you. It's worth the effort to talk with the IT people, if that's what it takes to get it.

        Comment


        • #5
          Thanks Clyde - you are right I do not want to put Var2 and N back into the original data set.

          By way of background: var1a, var1b are preferences. If the first preference was a laptop, this is recorded as 1 under var1a. If the second preference is a pc, this is recorded as 2 under var1b.

          Ultimately, I just want to create one table that shows how many times1, 2, 3, 4 and 5 were mentioned across var1a and var1b. Even though there are 200 respondents it is possible to have 350 observations across the two variables. This would allow me to understand how strong some preferences are.

          I have been trying -collapse- (and -tab-), albeit unsuccessfully

          I will look into getting -dataex- installed.

          Thanks again.

          Comment


          • #6
            I have to support Clyde on using dataex. It's in your own interests. A clear question here can be answered in seconds; an unclear question will often get no answer at all or at best be answered slowly and entail extended dialogue.

            Your problem is a tabulation problem, not one of creating new variables.

            Search this forum for mentions of tabm as one possible solution to your problem. But you must install it from tab_chi (SSC). So, you may have the same problem there.

            Comment


            • #7
              Many thanks Clyde and Nick.

              Both codes work a charm.

              Comment

              Working...
              X