Announcement

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

  • Data management problem

    Hi

    I am trying to analyze some data, and have entered some commands,..as reproduced below,..however when i get to the loop in red,..i'm getting "==1 invalid name" and have run down quite a number of answers on similar problems, but i am not managing to find the problem. Help would be much appreciated,..i have also attached the excel file so that the commands can be reproduced.

    // Importing data
    import excel "Version3_20_06.xlsx", sheet("Form responses 1") firstrow

    // Cleaning data

    // Store-level variables
    // Filling in store level variables
    replace Retail_size="Small" if Retail_size=="small"
    replace Retail_size=Retail_size[_n-1] if Retail_size==""
    egen store_number=group(StoresTimestamp)
    replace store_number=store_number[_n-1] if store_number==.
    replace Shop_location=Shop_location[_n-1] if Shop_location==""

    // Assigning numeric values to categorical variables
    encode Retail_size, gen(shop_size)
    encode Shop_location, gen(shop_location)
    // Reordering variables
    order store_number shop_size shop_location

    // Dropping un-needed variables
    drop StoresTimestamp Retail_size Shop_location


    // Brand-level variables
    drop Category_beverage
    rename Cocacola_available cocacola_available
    rename Cocacola_price cocacola_price
    rename Coke_light_available coke_light_available
    rename Coke_light_price coke_light_price
    rename sprite_zeroavailable sprite_zero_available

    //rename sprite_Size sprite_size
    //rename BeverageSize stoney_size
    rename cocacola_Size cocacola_size
    rename Coo_ee_Price Coo_ee_price
    rename CloverTropikajuicePrice clover_tropika_juice_price
    rename cococola_promotion cocacola_promotion

    //rename Stoney_zero_available stoney_zero_available
    rename Stoney_zero_price stoney_zero_price
    rename Stoney_zero_promotion stoney_zero_promotion

    // Encode availability

    foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
    encode `var'_available, gen(`var'_available_)
    drop `var'_available
    rename `var'_available_ `var'_available
    }
    // Generating size measure in litres
    foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
    gen `var'_size_litres=.
    replace `var'_size_litres=0.250 if `var'_size=="> 250ml"
    replace `var'_size_litres=0.300 if `var'_size=="> 300ml"
    replace `var'_size_litres=0.330 if `var'_size=="> 330ml"
    replace `var'_size_litres=0.355 if `var'_size=="> 355ml"
    replace `var'_size_litres=0.440 if `var'_size=="> 440ml"
    replace `var'_size_litres=0.473 if `var'_size=="> 473ml"
    replace `var'_size_litres=0.500 if `var'_size=="> 500ml"
    replace `var'_size_litres=0.750 if `var'_size=="> 750ml"
    replace `var'_size_litres=1 if `var'_size=="1L"
    replace `var'_size_litres=1.25 if `var'_size=="1> .25L"
    replace `var'_size_litres=1.5 if `var'_size=="1.> 5L"
    replace `var'_size_litres=2 if `var'_size=="2L"
    replace `var'_size_litres=2.25 if `var'_size=="2 > .25L"
    drop `var'_size
    }
    // Generating price per litre
    destring coke_zero_price, replace force float
    destring sprite_zero_price, replace force float
    destring red_bull_sugarfree_price, replace force float

    foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
    gen `var'_price_litre=`var'_price/`var'_size_litres
    }
    // Re-shaping data for analysis
    order store_number-flavoured_water_price_litre, alpha
    order store_number shop_size shop_location
    local i=1

    foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruit_juice clover_tropika_juice pure_milk flavoured_water {
    preserve
    if `i'==1 {
    keep store_number shop_size shop_location `var'*
    drop if `var'_size_litres==.
    rename `var'_available available
    rename `var'_price price
    rename `var'_price_litre price_litre
    rename `var'_size_litres size_litres
    rename `var'_promotion promotion
    gen brand="`var'"
    keep store_number shop_size shop_location brand size_litres available price price_litre promotion
    order store_number shop_size shop_location brand size_litres available price price_litre promotion
    save "Data.dta", replace
    }

    if `i'>1 {
    keep store_number shop_size shop_location `var'*
    drop if `var'_size_litres==.
    rename `var'_available available
    rename `var'_price price
    rename `var'_price_litre price_litre
    rename `var'_size_litres size_litres
    rename `var'_promotion promotion
    gen brand="`var'"
    keep store_number shop_size shop_location brand size_litres available price price_litre promotion
    order store_number shop_size shop_location brand size_litres available price price_litre promotion
    save "Temp/`var'.dta", replace
    use "Data.dta", clear
    append using "Temp/`var'.dta", force
    save "Data.dta", replace
    }
    local i=`i'+1
    restore
    }


    I can't seem to find a solution to the "==1 invalid name" error,..i will greatly appreciate any possible help
    Attached Files

  • #2
    Welcome to Statalist.

    I believe your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting a few lines and running them, then selecting the next few lines and running them, and so on.

    Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
    Code:
    . do "/Users/lisowskiw/Downloads/example.do"
    
    . local message Hello, world.
    
    . display "The message is `message'"
    The message is Hello, world.
    
    . 
    end of do-file
    Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
    Code:
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
    
    . local message Hello, world.
    
    . 
    end of do-file
    
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
    
    . display "The message is `message'"
    The message is 
    
    . 
    end of do-file
    The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

    In your case, I think you selected and ran lines ending with
    Code:
    local i=1
    and then selected the entire foreach loop to run. But once the code that included
    Code:
    local i=1
    finished, the value of the local macro i vanished, so in the foreach loop the command
    Code:
    if `i'==1 {
    was interpreted as
    Code:
    if ==1 {

    Comment


    • #3
      Thank you so much William, you clearly pinpointed exactly what i did,..and highlighted an important point,..my concept of separate do files was way too limited,..i really ran the files separately just as you pinpointed, am sure, the program will run smoothly if i run the entire do file.

      Thank you so much

      Comment


      • #4
        Besides the preceding, I noted something strange the understanding of which might improve your programming: In the code you show, you set the local i = 1, and then test for whether it is == 1. This does not seem like what you really want, since `i' will *always" be equal to 1 at that point in your code. If `i' is always 1, why have an "if" to test it? Similarly, when you later test for `i' > 1, that is strange, since there is nothing in your code to change the value of `i' before that point, so it must still be ==1. You may well now understand what your confusion was, so that this is irrelevant to you now.
        Last edited by Mike Lacy; 01 Oct 2019, 06:50.

        Comment


        • #5
          Mike Lacy -

          Because the code in post #1 was not posted using CODE delimiters, any indenting that might have made the looping easier to read was lost. Here's an outline of the foreach block that helps clarify the code.
          Code:
          foreach var in cocacola ... {
              preserve
          
              if `i'==1 {
                  ...
              }
          
              if `i'>1 {
                  ...
              }
          
              local i=`i'+1
              restore
          }


          Rushambwat -

          To improve the presentation of your future posts, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Comment


          • #6
            Aha, ok I get it re the value of `i' -- my oversight. If/else rather than if/if would be a more conventional usage there, too, besides the indenting.

            Comment

            Working...
            X