Announcement

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

  • Using program

    Hi, this is my code. I need help in making it succint and reproducible. I want only three values should be inputted in the program veg_lower_rate_qty_1 sale_unit_1 crop_1. The program take 3 var from the file units_cf.xls: unit, crop and conversion. Unit, crop and conversion stays consistent for running the program. Is there a way to feed these 3 var in the program?

    Second, I want to run the loop _N times as in units_cf.xlsx dataset.

    Thanks in advance for your help

    use "`input'\hfms.dta", clear

    //creating a temp index number to merge 2 files
    gen order1=_n
    cap drop _m
    tempfile m2
    save `m2',replace

    import excel "`input_1'\units_cf.xlsx", sheet("fruits_vegetables") firstrow clear
    replace order1=_n
    rename sale_unit unit
    merge 1:1 order1 using `m2'



    program define qtyconvert
    *version 8
    args qty data_unit data_crop data unit crop conversion
    confirm var `qty' `data_unit' `data_crop' `unit' `crop' `conversion'

    forval k=1/17{
    replace `qty'= `qty' if `unit'=="kilo"
    replace `qty' = `qty'*`conversion' if `data_unit'==`unit'[`k'] & `data_crop'==`crop'[`k'] & !mi(`qty')
    }
    end

    set trace on
    qtyconvert veg_lower_rate_qty_1 sale_unit_1 crop_1 unit crop conversion

  • #2
    Your post is not very clear. Is there a reason the variable is defined as a local like `qty' with the slant comma and inverted comma?
    Use code delimiters please. Also, show what error you are getting.

    Comment


    • #3
      Hi Parul Gupta, thanks for your comment! the code is not showing any error. It do want to re-write the code so that the program "qtyconvert" does not need 6 arguments. I want to change it so that it works with only 3 args which are variable (veg_lower_rate_qty_1 sale_unit_1 crop_1).

      Comment


      • #4
        Dimple, did you figure this out?

        If not, as Parul indicated, it would be good to have some more clarity on your problem. It would help to see an excerpt of the units_cf.xlsx file, and also what your dataset in memory looks like just before you are running the program. Use the dataex command to show the data extract.

        Comment


        • #5
          No, I have not been able to figure this.

          HFMS dataset: (this is a snapshot of the dataset, could not get all the variables in the dataset)
          dataex veg_lower_rate_qty_1 sale_unit_1 crop_1 veg_lower_rate_qty_2 sale_unit_2 crop_2 surveyor_id vendor_id

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int veg_lower_rate_qty_1 str173 sale_unit_1 str143 crop_1 int veg_lower_rate_qty_2 str173 sale_unit_2 str143 crop_2 int surveyor_id str14 vendor_id
           0 "kilo"  "Bitter Gourd"  2 "kilo"  "Cucumber"        103 "d2024020910709"
           . "."     ""              . "."     ""                103 "d2024020610704"
           . "."     ""              . "."     ""                103 "d2024021210704"
           0 "kilo"  "Green Chilli"  0 "kilo"  "Ridged Gourd"    102 "d2024021310801"
           . ""      ""              . ""      ""                101 "d2024021210601"
           0 "kilo"  "Grape"         . ""      ""                102 "d2024020610606"
           . ""      ""              . ""      ""                102 "d2024020910607"
           0 "kilo"  "Green Chilli"  0 "kilo"  "Ridged Gourd"    102 "d2024021310801"
           . "."     ""              . "."     ""                102 "d2024021710801"
           . ""      ""              . ""      ""                101 "d2024021710703"
           0 "kilo"  "Ripe Mango"   10 "kilo"  "Apple"           103 "d2024020610702"
           0 "kilo"  "Ripe Mango"    0 "kilo"  "Apple"           103 "d2024020610702"
           0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        102 "d2024021210805"
           0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        102 "d2024021210805"
           . "."     ""              . "."     ""                102 "d2024021310805"
           0 "kilo"  "Green Chilli"  0 "kilo"  "Ivy Gourd"       102 "d2024021511307"
           1 "dozen" "Ripe Banana"   2 "kilo"  "Apple"           102 "d2024021010601"
           . "."     ""              . "."     ""                103 "d2024021010706"
           0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        103 "d2024020910709"
           . "."     ""              . "."     ""                101 "d2024021311305"
           0 "kilo"  "Bitter Gourd"  5 "kilo"  "Cucumber"        103 "d2024020910708"
           . "."     ""              . "."     ""                102 "d2024020610604"
           . ""      ""              . ""      ""                101 "d2024020610802"
           0 "kilo"  "Cucumber"      0 "kilo"  "Bean"            101 "d2024021210602"
           . ""      ""              . ""      ""                102 "d2024021710701"
           0 "dozen" "Ripe Banana"   0 "kilo"  "Grape"           103 "d2024021510607"
           . "dozen" "Ripe Banana"   . "kilo"  "Apple"           102 "d2024021010601"
           0 "kilo"  "Cucumber"     10 "kilo"  "Bean"            101 "d2024021210602"
           . "."     ""              . "."     ""                102 "d2024021710802"
           0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        103 "d2024020910708"
           . "."     ""              . "."     ""                101 "d2024021210605"
           . "."     ""              . "."     ""                102 "d2024021510805"
           . ""      ""              . ""      ""                102 "d2024020910607"
           . "."     ""              . "."     ""                102 "d2024021510802"
           . "."     ""              . "."     ""                103 "d2024021210701"
           0 "dozen" "Ripe Banana"   0 "kilo"  "Grape"           103 "d2024021510607"
           . "."     ""              . "."     ""                103 "d2024021010703"
           . "."     ""              . "."     ""                102 "d2024021510808"
          10 "kilo"  "Ivy Gourd"     5 "kilo"  "Tomato"          101 "d2024021510602"
           0 "dozen" "Ripe Banana"   0 "kilo"  "Apple"           103 "d2024021510604"
           . ""      ""              . ""      ""                101 "d2024021210607"
           . "."     ""              . "."     ""                101 "d2024021310609"
           . ""      ""              . ""      ""                101 "d2024021010801"
           . "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610801"
           . ""      ""              . ""      ""                101 "d2024021710703"
           . "."     ""              . "."     ""                102 "d2024020910609"
           . ""      ""              . ""      ""                103 "d2024021510606"
           0 "kilo"  "Raw Papaya"    0 "kilo"  "Potato"          101 "d2024021311306"
           . "."     ""              . "."     ""                103 "d2024021010704"
           . ""      ""              . ""      ""                103 "d2024021510606"
           . ""      ""              . ""      ""                101 "d2024021010602"
           . "."     ""              . "."     ""                101 "d2024021310601"
           0 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610804"
           . "."     ""              . "."     ""                103 "d2024020910703"
           0 "dozen" "Ripe Banana"   0 "kilo"  "Apple"           101 "d2024021310606"
           0 "kilo"  "Carrot"       10 "kilo"  "Cauliflower"     103 "d2024021010701"
           0 "dozen" "Ripe Banana"   1 "kilo"  "Apple"           103 "d2024021510604"
           . ""      ""              . ""      ""                101 "d2024021210607"
           0 "kilo"  "Grape"         . ""      ""                102 "d2024020610606"
           0 "kilo"  "Green Chilli"  0 "kilo"  "Ivy Gourd"       102 "d2024021511307"
           . ""      ""              . ""      ""                101 "d2024021010801"
           . ""      ""              . ""      ""                101 "d2024020610802"
           . "dozen" "Ripe Banana"   . "kilo"  "Apple"           101 "d2024021310606"
          15 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610801"
           . ""      ""              . ""      ""                102 "d2024021710701"
           0 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610804"
           . ""      ""              . ""      ""                101 "d2024021010602"
           . ""      ""              . ""      ""                101 "d2024021210601"
           . ""      ""              . ""      ""                113 "d2024020610403"
           . ""      ""              . ""      ""                101 "d2024022210303"
           . ""      ""              . ""      ""                103 "d2024020910105"
           . ""      ""              . ""      ""                113 "d2024020910105"
           . ""      ""              . ""      ""                113 "d2024021711301"
           . ""      ""              . ""      ""                102 "d2024021210497"
           . ""      ""              . ""      ""                103 "d2024022010204"
           3 "kilo"  "Ridged Gourd"  0 "bunch" "Leafy Vegetable" 112 "d2024021711202"
           . ""      ""              . ""      ""                101 "d2024022210307"
           . ""      ""              . ""      ""                113 "d2024022010307"
           . ""      ""              . ""      ""                104 "d2024021711201"
           . ""      ""              . ""      ""                104 "d2024021010107"
           . ""      ""              . ""      ""                103 "d2024021710208"
           . ""      ""              . ""      ""                112 "d2024022010206"
           . ""      ""              . ""      ""                112 "d2024022010206"
           0 "kilo"  "Brinjal"       0 "kilo"  "Lady Finger"     112 "d2024022010112"
           . ""      ""              . ""      ""                103 "d2024021710203"
           0 "kilo"  "Green Chilli"  3 "piece" "Bottle Gourd"    103 "d2024020910206"
           . ""      ""              . ""      ""                103 "d2024021710206"
           . ""      ""              . ""      ""                102 "d2024021010406"
          30 "kilo"  "Cucumber"     80 "kilo"  "Capsicum"        104 "d2024022010410"
           4 "kilo"  "Potato"        0 "kilo"  "Garlic"          102 "d2024022010397"
           . ""      ""              . ""      ""                112 "d2024021711207"
           0 "kilo"  "Green Chilli"  0 "piece" "Bottle Gourd"    103 "d2024020910206"
           . ""      ""              . ""      ""                101 "d2024022210307"
           0 "kilo"  "Green Chilli" 20 "piece" "Lemon     "          101 "d2024022011207"
           . ""      ""              . ""      ""                102 "d2024022210305"
           . ""      ""              . ""      ""                103 "d2024020610202"
           . ""      ""              . ""      ""                101 "d2024021710407"
           0 "Bhaga" "Green Chilli"  0 "kilo"  "Ivy Gourd"       103 "d2024020610205"
           . ""      ""              . ""      ""                101 "d2024020910305"
           . ""      ""              . ""      ""                104 "d2024020610103"
          end
          units_cf.xlsx
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte order1 str21 crop str14 sale_unit double conversion
          1 "Bottle Gourd"          "piece"            1
          1 "Leafy Vegetable"       "bunch"           .2
          1 "Lemon        "                "piece"          .03
          1 "Green Chilli"          "bunch"           .2
          1 "Pineapple"             "piece"           .4
          1 "DHANIA PARRA"          "bunch"           .2
          1 "Green Chilli"          "Bhaga"           .2
          1 "DHANIA PATRA"          "bunch"           .2
          1 "Green Chilli"          "Bhaga 10 tanka"  .2
          1 "Ripe Banana"           "dozen"          1.5
          1 "Snake Bean/Long Beans" "bunch"            1
          1 "Watermelon"            "piece"          2.5
          1 "Ripe Papaya"           "piece"            1
          1 "Jackfruit"             "piece"            2
          1 "Raw Papaya"            "piece"            1
          1 "Pumpkin"               "piece"            2
          1 "Lemon        "                "Bhaga"           .2
          end
          Last edited by Dimple khattar khattar; 29 Jul 2024, 00:46.

          Comment


          • #6
            A different and perhaps more efficient way of doing this might be to reshape the data file and then merge with the units file. Perhaps something like this:

            Code:
            clear
            input byte order1 str21 crop str14 sale_unit double conversion
            1 "Bottle Gourd"          "piece"            1
            1 "Leafy Vegetable"       "bunch"           .2
            1 "Lemon        "                "piece"          .03
            1 "Green Chilli"          "bunch"           .2
            1 "Pineapple"             "piece"           .4
            1 "DHANIA PARRA"          "bunch"           .2
            1 "Green Chilli"          "Bhaga"           .2
            1 "DHANIA PATRA"          "bunch"           .2
            1 "Green Chilli"          "Bhaga 10 tanka"  .2
            1 "Ripe Banana"           "dozen"          1.5
            1 "Snake Bean/Long Beans" "bunch"            1
            1 "Watermelon"            "piece"          2.5
            1 "Ripe Papaya"           "piece"            1
            1 "Jackfruit"             "piece"            2
            1 "Raw Papaya"            "piece"            1
            1 "Pumpkin"               "piece"            2
            1 "Lemon        "                "Bhaga"           .2
            end
            
            replace crop = trim(crop)
            tempfile units
            save `units'
            
            clear
            input int veg_lower_rate_qty_1 str173 sale_unit_1 str143 crop_1 int veg_lower_rate_qty_2 str173 sale_unit_2 str143 crop_2 int surveyor_id str14 vendor_id
             0 "kilo"  "Bitter Gourd"  2 "kilo"  "Cucumber"        103 "d2024020910709"
             . "."     ""              . "."     ""                103 "d2024020610704"
             . "."     ""              . "."     ""                103 "d2024021210704"
             0 "kilo"  "Green Chilli"  0 "kilo"  "Ridged Gourd"    102 "d2024021310801"
             . ""      ""              . ""      ""                101 "d2024021210601"
             0 "kilo"  "Grape"         . ""      ""                102 "d2024020610606"
             . ""      ""              . ""      ""                102 "d2024020910607"
             0 "kilo"  "Green Chilli"  0 "kilo"  "Ridged Gourd"    102 "d2024021310801"
             . "."     ""              . "."     ""                102 "d2024021710801"
             . ""      ""              . ""      ""                101 "d2024021710703"
             0 "kilo"  "Ripe Mango"   10 "kilo"  "Apple"           103 "d2024020610702"
             0 "kilo"  "Ripe Mango"    0 "kilo"  "Apple"           103 "d2024020610702"
             0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        102 "d2024021210805"
             0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        102 "d2024021210805"
             . "."     ""              . "."     ""                102 "d2024021310805"
             0 "kilo"  "Green Chilli"  0 "kilo"  "Ivy Gourd"       102 "d2024021511307"
             1 "dozen" "Ripe Banana"   2 "kilo"  "Apple"           102 "d2024021010601"
             . "."     ""              . "."     ""                103 "d2024021010706"
             0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        103 "d2024020910709"
             . "."     ""              . "."     ""                101 "d2024021311305"
             0 "kilo"  "Bitter Gourd"  5 "kilo"  "Cucumber"        103 "d2024020910708"
             . "."     ""              . "."     ""                102 "d2024020610604"
             . ""      ""              . ""      ""                101 "d2024020610802"
             0 "kilo"  "Cucumber"      0 "kilo"  "Bean"            101 "d2024021210602"
             . ""      ""              . ""      ""                102 "d2024021710701"
             0 "dozen" "Ripe Banana"   0 "kilo"  "Grape"           103 "d2024021510607"
             . "dozen" "Ripe Banana"   . "kilo"  "Apple"           102 "d2024021010601"
             0 "kilo"  "Cucumber"     10 "kilo"  "Bean"            101 "d2024021210602"
             . "."     ""              . "."     ""                102 "d2024021710802"
             0 "kilo"  "Bitter Gourd"  0 "kilo"  "Cucumber"        103 "d2024020910708"
             . "."     ""              . "."     ""                101 "d2024021210605"
             . "."     ""              . "."     ""                102 "d2024021510805"
             . ""      ""              . ""      ""                102 "d2024020910607"
             . "."     ""              . "."     ""                102 "d2024021510802"
             . "."     ""              . "."     ""                103 "d2024021210701"
             0 "dozen" "Ripe Banana"   0 "kilo"  "Grape"           103 "d2024021510607"
             . "."     ""              . "."     ""                103 "d2024021010703"
             . "."     ""              . "."     ""                102 "d2024021510808"
            10 "kilo"  "Ivy Gourd"     5 "kilo"  "Tomato"          101 "d2024021510602"
             0 "dozen" "Ripe Banana"   0 "kilo"  "Apple"           103 "d2024021510604"
             . ""      ""              . ""      ""                101 "d2024021210607"
             . "."     ""              . "."     ""                101 "d2024021310609"
             . ""      ""              . ""      ""                101 "d2024021010801"
             . "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610801"
             . ""      ""              . ""      ""                101 "d2024021710703"
             . "."     ""              . "."     ""                102 "d2024020910609"
             . ""      ""              . ""      ""                103 "d2024021510606"
             0 "kilo"  "Raw Papaya"    0 "kilo"  "Potato"          101 "d2024021311306"
             . "."     ""              . "."     ""                103 "d2024021010704"
             . ""      ""              . ""      ""                103 "d2024021510606"
             . ""      ""              . ""      ""                101 "d2024021010602"
             . "."     ""              . "."     ""                101 "d2024021310601"
             0 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610804"
             . "."     ""              . "."     ""                103 "d2024020910703"
             0 "dozen" "Ripe Banana"   0 "kilo"  "Apple"           101 "d2024021310606"
             0 "kilo"  "Carrot"       10 "kilo"  "Cauliflower"     103 "d2024021010701"
             0 "dozen" "Ripe Banana"   1 "kilo"  "Apple"           103 "d2024021510604"
             . ""      ""              . ""      ""                101 "d2024021210607"
             0 "kilo"  "Grape"         . ""      ""                102 "d2024020610606"
             0 "kilo"  "Green Chilli"  0 "kilo"  "Ivy Gourd"       102 "d2024021511307"
             . ""      ""              . ""      ""                101 "d2024021010801"
             . ""      ""              . ""      ""                101 "d2024020610802"
             . "dozen" "Ripe Banana"   . "kilo"  "Apple"           101 "d2024021310606"
            15 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610801"
             . ""      ""              . ""      ""                102 "d2024021710701"
             0 "dozen" "Ripe Banana"   . ""      ""                101 "d2024020610804"
             . ""      ""              . ""      ""                101 "d2024021010602"
             . ""      ""              . ""      ""                101 "d2024021210601"
             . ""      ""              . ""      ""                113 "d2024020610403"
             . ""      ""              . ""      ""                101 "d2024022210303"
             . ""      ""              . ""      ""                103 "d2024020910105"
             . ""      ""              . ""      ""                113 "d2024020910105"
             . ""      ""              . ""      ""                113 "d2024021711301"
             . ""      ""              . ""      ""                102 "d2024021210497"
             . ""      ""              . ""      ""                103 "d2024022010204"
             3 "kilo"  "Ridged Gourd"  0 "bunch" "Leafy Vegetable" 112 "d2024021711202"
             . ""      ""              . ""      ""                101 "d2024022210307"
             . ""      ""              . ""      ""                113 "d2024022010307"
             . ""      ""              . ""      ""                104 "d2024021711201"
             . ""      ""              . ""      ""                104 "d2024021010107"
             . ""      ""              . ""      ""                103 "d2024021710208"
             . ""      ""              . ""      ""                112 "d2024022010206"
             . ""      ""              . ""      ""                112 "d2024022010206"
             0 "kilo"  "Brinjal"       0 "kilo"  "Lady Finger"     112 "d2024022010112"
             . ""      ""              . ""      ""                103 "d2024021710203"
             0 "kilo"  "Green Chilli"  3 "piece" "Bottle Gourd"    103 "d2024020910206"
             . ""      ""              . ""      ""                103 "d2024021710206"
             . ""      ""              . ""      ""                102 "d2024021010406"
            30 "kilo"  "Cucumber"     80 "kilo"  "Capsicum"        104 "d2024022010410"
             4 "kilo"  "Potato"        0 "kilo"  "Garlic"          102 "d2024022010397"
             . ""      ""              . ""      ""                112 "d2024021711207"
             0 "kilo"  "Green Chilli"  0 "piece" "Bottle Gourd"    103 "d2024020910206"
             . ""      ""              . ""      ""                101 "d2024022210307"
             0 "kilo"  "Green Chilli" 20 "piece" "Lemon     "          101 "d2024022011207"
             . ""      ""              . ""      ""                102 "d2024022210305"
             . ""      ""              . ""      ""                103 "d2024020610202"
             . ""      ""              . ""      ""                101 "d2024021710407"
             0 "Bhaga" "Green Chilli"  0 "kilo"  "Ivy Gourd"       103 "d2024020610205"
             . ""      ""              . ""      ""                101 "d2024020910305"
             . ""      ""              . ""      ""                104 "d2024020610103"
            end
            
            gen `c(obs_t)' obnum = _n // this is to enable the reshape
            reshape long veg_lower_rate_qty_ sale_unit_ crop_, i(surveyor_id vendor_id obnum) j(num)
            rename *_ *
            replace crop = trim(crop)
            
            merge m:1 crop sale_unit using `units', keepusing(conversion) keep(master match)
            compress
            Diagnostics to check everything is converted:

            Code:
            // diagnostics
            gen byte missing_conversion = _merge == 1 & !(sale_unit == "kilo" | veg_lower_rate_qty == .)
            
            assert !missing_conversion
            drop missing_conversion
            Now do the conversion and reshape the data back to its original form:

            Code:
            // convert the quantity variable
            gen veg_lower_rate_qty_kilo = veg_lower_rate_qty * conversion if !missing(conversion)
            replace veg_lower_rate_qty_kilo = veg_lower_rate_qty if sale_unit == "kilo"
            
            // return data to its original shape
            drop _merge conversion veg_lower_rate_qty
            rename (veg_lower_rate_qty_kilo sale_unit crop) =_
            reshape wide veg_lower_rate_qty_kilo_ sale_unit_ crop_, i(surveyor_id vendor_id obnum) j(num)
            sort obnum
            drop obnum
            Last edited by Hemanshu Kumar; 29 Jul 2024, 09:04.

            Comment

            Working...
            X