Announcement

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

  • question about geting the total amount based on several catagories

    Hello everyone.
    I hope you are doing very well.
    there are several food items. two questions are asked about each of them. One was asked about their consumed amount in a reference period. The next one is a categorical variable which is asked about the source of those foods. So the question is I want to get the amount of each item by the categories of categorical variable in a short command.
    the sample of data is as below. q_23_8_* are sources of foods and q_23_9_* are the consumed amounts of foods. the letters (a-e) at the end of questions names represent the food names.
    q_23_8_a q_23_9_a q_23_8_b q_23_9_b q_23_8_c q_23_9_c q_23_8_d q_23_9_d q_23_8_e q_23_9_e
    Purchased 3 own production 1 Purchased 22 own production 120 Purchased 2
    own production 1 Purchased 2.5 Purchased 28 borrowed 20 Bartered 15
    Bartered 2 Bartered 1 Bartered 13 Purchased 50 Purchased 7
    borrowed 4 own production 0.6 own production 3 Bartered 20 Purchased 3
    Bartered 1.5 Purchased 0.5 Purchased 14 Bartered 14 own production 8
    food aid 1.2 Purchased 1.2 food aid 16 Purchased 30 food aid 10
    own production 10 Bartered 3 own production 21.5 food aid 45 borrowed 6
    food aid 1.2 food aid 0.6 Purchased 49 Purchased 65 Purchased 11
    borrowed 1 Purchased 2 Purchased 3 own production 45 own production 9










  • #2
    Taqi:
    this seems a job for -collapse-.
    As an aside, please use CODE delimiters and -dataex- to help others elaborate on your data example (see the FAQ on that). Thanks.
    Last edited by Carlo Lazzaro; 30 Jul 2024, 02:38.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Here is the dataset as you ought to have provided it (see FAQ):

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str14 q_23_8_a float q_23_9_a str14 q_23_8_b float q_23_9_b str14 q_23_8_c float q_23_9_c str14 q_23_8_d int q_23_9_d str14 q_23_8_e byte q_23_9_e
      "Purchased"        3 "own production"   1 "Purchased"        22 "own production" 120 "Purchased"       2
      "own production"   1 "Purchased"      2.5 "Purchased"        28 "borrowed"        20 "Bartered"       15
      "Bartered"         2 "Bartered"         1 "Bartered"         13 "Purchased"       50 "Purchased"       7
      "borrowed"         4 "own production"  .6 "own production"    3 "Bartered"        20 "Purchased"       3
      "Bartered"       1.5 "Purchased"       .5 "Purchased"        14 "Bartered"        14 "own production"  8
      "food aid"       1.2 "Purchased"      1.2 "food aid"         16 "Purchased"       30 "food aid"       10
      "own production"  10 "Bartered"         3 "own production" 21.5 "food aid"        45 "borrowed"        6
      "food aid"       1.2 "food aid"        .6 "Purchased"        49 "Purchased"       65 "Purchased"      11
      "borrowed"         1 "Purchased"        2 "Purchased"         3 "own production"  45 "own production"  9
      end

      Now consider this code:

      Code:
      gen `c(obs_t)' id = _n // creating dummy identifier since none was provided
      reshape long q_23_8_ q_23_9_ , i(id) j(food) string
      
      collapse (sum) q_23_9_, by(id q_23_8_)
      
      encode q_23_8_, gen(source)
      drop q_23_8_
      
      rename q_23_9_ amount_
      
      // preserve labels to recover later
      levelsof source, local(sourcevals)
      foreach val of local sourcevals {
          local lab`val': label source `val'
      }
      
      // revert to wide format
      reshape wide amount_ , i(id) j(source)
      // recover labels
      foreach val of local sourcevals {
          label var amount_`val' `"`lab`val''"'
      }
      which produces:

      Code:
      . list, noobs sep(0)
      
        +-----------------------------------------------------------+
        | id   amount_1   amount_2   amount_3   amount_4   amount_5 |
        |-----------------------------------------------------------|
        |  1          .         27          .          .        121 |
        |  2         15       30.5         20          .          1 |
        |  3         16         57          .          .          . |
        |  4         20          3          4          .        3.6 |
        |  5       15.5       14.5          .          .          8 |
        |  6          .       31.2          .       27.2          . |
        |  7          3          .          6         45       31.5 |
        |  8          .        125          .        1.8          . |
        |  9          .          5          1          .         54 |
        +-----------------------------------------------------------+
      where

      Code:
      . d
      
      Contains data
       Observations:             9                  
          Variables:             6                  
      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      id              byte    %8.0g                
      amount_1        double  %9.0g                 Bartered
      amount_2        double  %9.0g                 Purchased
      amount_3        double  %9.0g                 borrowed
      amount_4        double  %9.0g                 food aid
      amount_5        double  %9.0g                 own production
      ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Sorted by: id
           Note: Dataset has changed since last saved.
      Last edited by Hemanshu Kumar; 30 Jul 2024, 03:05.

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Taqi:
        this seems a job for -collapse-.
        As an aside, please use CODE delimiters and -dataex- to help others elaborate on your data example (see the FAQ on that). Thanks.
        Thank Mr.Carlo for your recommendations and I will consider them in the future.

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          Here is the dataset as you ought to have provided it (see FAQ):

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str14 q_23_8_a float q_23_9_a str14 q_23_8_b float q_23_9_b str14 q_23_8_c float q_23_9_c str14 q_23_8_d int q_23_9_d str14 q_23_8_e byte q_23_9_e
          "Purchased" 3 "own production" 1 "Purchased" 22 "own production" 120 "Purchased" 2
          "own production" 1 "Purchased" 2.5 "Purchased" 28 "borrowed" 20 "Bartered" 15
          "Bartered" 2 "Bartered" 1 "Bartered" 13 "Purchased" 50 "Purchased" 7
          "borrowed" 4 "own production" .6 "own production" 3 "Bartered" 20 "Purchased" 3
          "Bartered" 1.5 "Purchased" .5 "Purchased" 14 "Bartered" 14 "own production" 8
          "food aid" 1.2 "Purchased" 1.2 "food aid" 16 "Purchased" 30 "food aid" 10
          "own production" 10 "Bartered" 3 "own production" 21.5 "food aid" 45 "borrowed" 6
          "food aid" 1.2 "food aid" .6 "Purchased" 49 "Purchased" 65 "Purchased" 11
          "borrowed" 1 "Purchased" 2 "Purchased" 3 "own production" 45 "own production" 9
          end

          Now consider this code:

          Code:
          gen `c(obs_t)' id = _n // creating dummy identifier since none was provided
          reshape long q_23_8_ q_23_9_ , i(id) j(food) string
          
          collapse (sum) q_23_9_, by(id q_23_8_)
          
          encode q_23_8_, gen(source)
          drop q_23_8_
          
          rename q_23_9_ amount_
          
          // preserve labels to recover later
          levelsof source, local(sourcevals)
          foreach val of local sourcevals {
          local lab`val': label source `val'
          }
          
          // revert to wide format
          reshape wide amount_ , i(id) j(source)
          // recover labels
          foreach val of local sourcevals {
          label var amount_`val' `"`lab`val''"'
          }
          which produces:

          Code:
          . list, noobs sep(0)
          
          +-----------------------------------------------------------+
          | id amount_1 amount_2 amount_3 amount_4 amount_5 |
          |-----------------------------------------------------------|
          | 1 . 27 . . 121 |
          | 2 15 30.5 20 . 1 |
          | 3 16 57 . . . |
          | 4 20 3 4 . 3.6 |
          | 5 15.5 14.5 . . 8 |
          | 6 . 31.2 . 27.2 . |
          | 7 3 . 6 45 31.5 |
          | 8 . 125 . 1.8 . |
          | 9 . 5 1 . 54 |
          +-----------------------------------------------------------+
          where

          Code:
          . d
          
          Contains data
          Observations: 9
          Variables: 6
          ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
          Variable Storage Display Value
          name type format label Variable label
          ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
          id byte %8.0g
          amount_1 double %9.0g Bartered
          amount_2 double %9.0g Purchased
          amount_3 double %9.0g borrowed
          amount_4 double %9.0g food aid
          amount_5 double %9.0g own production
          ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
          Sorted by: id
          Note: Dataset has changed since last saved.
          Thank you Mr.Kumar for helping me. The commands worked well.

          Comment

          Working...
          X