Announcement

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

  • Using a loop to calculate new variable

    Hello all,

    I want to find the new sale price for each year represented through new_sales. I will use the sale price in 2000q4 as the baseline (100). For example, year=2001q1 should be calculated by: new_sales=0.08*100=8+100=108. Then, I want to use this calculated new_sales value (108) to calculate new_sales for 2001q2. Then, find the new_sales value in 2001q3 by using the value in 2001q2 and so on.

    I have many different new_sales columns that need to be calculated given different scenarios, so I think some sort of loop would work best.

    Here is an example of my data:

    Code:
    input str7 year float(sales_percent new_sales)
    2000q4 0.01 100
    2001q1 0.08
    2001q2 0.06
    2001q3 0.07
    2001q4 0.001
    2002q1 0.02
    2002q2 0.02
    2002q3 0.03
    2002q4 0.05
    2003q1 0.02
    2003q2 0.03
    2003q3 0.001
    2003q4 0.01
    I would appreciate any assistance with this!

    Thanks,
    Anoush K.


  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 year float(sales_percent new_sales)
    "2000q4"  .01 100
    "2001q1"  .08   .
    "2001q2"  .06   .
    "2001q3"  .07   .
    "2001q4" .001   .
    "2002q1"  .02   .
    "2002q2"  .02   .
    "2002q3"  .03   .
    "2002q4"  .05   .
    "2003q1"  .02   .
    "2003q2"  .03   .
    "2003q3" .001   .
    "2003q4"  .01   .
    end
    
    
    gen qdate = quarterly(year, "YQ")
    format qdate %tq
    tsset qdate
    replace new_sales = L1.new_sales*(1+sales_percent) if _n > 1
    Notes:
    1. Loops are often unnecessary in Stata where you would use them in other systems. This is such an instance. Stata has several mechanisms that work like loops but do not require establishing loop controls around a block of commands. This is one such case, where the natural observation-by-observation action of -replace- combined with a time-series operator (L1) does what might elsewhere be done with a loop. Whenever you think you need a loop in Stata, think again. Sometimes you will need one, but only a minority of the time.

    2. Typing up some text that loosely resembles -dataex- output does not produce satisfactory results. Copy what you showed in #1, paste it into the data editor, and try to run it--you will see what happens. No input and a cascade of error messages. If for whatever reason you need to post made-up rather than real data, make it up in Stata and then run -dataex-. Don't post ersatz -dataex-.

    3. The variable you called year is actually a quarterly date. That is perfectly legal syntax, but a bad idea. Sooner or later even you will mistakenly try to work with it as if it were really a year and garbage results will follow. Whenever possible, give variables names that suggest what they are. Sometimes that's not entirely feasible. But at least avoid variable names that are actually misleading.

    4. Dates as string variables are pretty useless in Stata. You can't calculate anything with them; you can't use them as time variables in any kind of analysis. Get used to converting all strings that represent dates or datetimes into real Stata internal format numerical date/datetime variables as one of the first things you do when you create a data set. Unless you will never use the string-date variable at all; in which case you may as well just drop it and save space in your hard drive.

    Comment


    • #3
      Clyde Schechter Thank you so much for your help. I will take into account these notes in my future Statalist posts.

      Thanks,
      Anoush K.

      Comment

      Working...
      X