Announcement

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

  • 0 real change made problem // if command

    Hi everyone , I have a problem with my code. the if and replace do not seem to work
    Here is my code :

    gen latitude=.
    replace latitude=11.464948199534232 if geo_bj2017==01 & geo_bj2011==01 & geo_bj2006==01 & year==2017 & year==2006 & year==2011
    replace latitude=10.716051499479113 if geo_bj2017==02 & geo_bj2011==02 & geo_bj2006==02 & year==2017 & year==2006 & year==2011
    replace latitude=6.54082899942049 if geo_bj2017==03 & geo_bj2011==03 & geo_bj2006==03 & year==2017 & year==2006 & year==2011
    replace latitude=9.709745299424814 if geo_bj2017==04 & geo_bj2011==04 & geo_bj2006==04 &year==2017 & year==2006 & year==2011
    replace latitude=8.108547899390357 if geo_bj2017==05 & geo_bj2011==05 & geo_bj2006==05 & year==2017 & year==2006 & year==2011

    Thank you for your help

  • #2
    the if and replace do not seem to work
    "Do not seem to work" is rather broad. Can you describe what does not work?

    Comment


    • #3
      Salma:
      as an aside to Ken's wise advice, take the first line of your code and replace the ampersand (-&-) with pipe (-|-).
      Does anything change?
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Ah... good find by Carlo. Perhaps use inlist. The year variable cannot be 2006 & 2011 & 2017 at the same time. Also, because you typed "01" instead of just "1", check also if those geo_bjYYYY variables are string or numeric as well.

        Code:
        gen latitude=.
        replace latitude=11.464948199534232 if geo_bj2017==01 & geo_bj2011==01 & geo_bj2006==01 & inlist(year, 2006, 2011, 2017)
        replace latitude=10.716051499479113 if geo_bj2017==02 & geo_bj2011==02 & geo_bj2006==02 & inlist(year, 2006, 2011, 2017)
        replace latitude=6.54082899942049   if geo_bj2017==03 & geo_bj2011==03 & geo_bj2006==03 & inlist(year, 2006, 2011, 2017)
        replace latitude=9.709745299424814  if geo_bj2017==04 & geo_bj2011==04 & geo_bj2006==04 & inlist(year, 2006, 2011, 2017)
        replace latitude=8.108547899390357  if geo_bj2017==05 & geo_bj2011==05 & geo_bj2006==05 & inlist(year, 2006, 2011, 2017)
        It'd be nice to know how those "geo_bjYYYY" variables work. For instance, if it's year 2017, what would be entered into geo_bj2006 and geo_bj2011? If they are entered as missing, then the above will not work either. Perhaps take a look at this:

        Code:
        foreach x in 2006 2011 2017{
        replace latitude=11.464948199534232 if geo_bj`x'==01 & year==`x'
        replace latitude=10.716051499479113 if geo_bj`x'==02 & year==`x'
        replace latitude=6.54082899942049   if geo_bj`x'==03 & year==`x'
        replace latitude=9.709745299424814  if geo_bj`x'==04 & year==`x'
        replace latitude=8.108547899390357  if geo_bj`x'==05 & year==`x'
        }
        Please also use dataex to post some sample data so that we can actually understand the challenge.
        Last edited by Ken Chui; 20 May 2022, 09:09.

        Comment


        • #5
          do you really have numeric entries such as "01"? this type of issue is why the FAQ strongly advises the giving of real(istic) data examples using -dataex-; if your variable is really a string variable, you need double quotes

          Comment


          • #6
            Cross-posted at https://stackoverflow.com/questions/...-work-on-stata

            Comment

            Working...
            X