Announcement

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

  • Advice

    SO am using a 151 variables 1500 observation dataset on STATA14 for my university project. so am trying to change the content of my all variable with the conditions using command " foreach"command but getting error 198 ,612 and 199
    my do file commands
    foreach gr* of varlist var G6-g162{
    generate gr= `gr(k)'
    replace gr(k)=0 if gr=0 & 1
    replace gr(k)=0.02 if gr=2
    replace gr(k)=0.07 if gr=3
    }
    please advice why am getting this error or what is the correct command for this operation.
    look for the output as
    612);
    . do "C:\Users\avd999\AppData\Local\Temp\STD01000000.tm p"

    . foreach gr* of varlist var G6-g162{
    2. generate gr= `gr(k)'
    3. replace gr(k)=0 if gr=0 & 1
    4. replace gr(k)=0.02 if gr=2
    5. replace gr(k)=0.07 if gr=3 {
    6.
    unexpected end of file
    r(612);

    end of do-file

    r(612);
    . foreach gr* of varlist var G6-g162{
    2.
    . generate gr= `gr(k)'
    3.
    . replace gr(k)=0 if gr=0 & 1
    4.
    . replace gr(k)=0.02 if gr=2
    5.
    . replace gr(k)=0.07 if gr=3}
    invalid syntax
    r(198);

    . replace gr(k)=0.07 if gr=3
    factor variables and time-series operators not allowed
    r(101);

    . replace gr(k)=0.07 if gr=3 }
    factor variables and time-series operators not allowed
    r(101);

    . foreach gr* of varlist var G6-g162{
    2.
    . generate gr= `gr(k)'
    3.
    . replace gr(k)=0 if gr=0 & 1
    4.
    . replace gr(k)=0.02 if gr=2
    5.
    . replace gr(k)=0.07 if gr=3
    6.
    . }
    invalid syntax
    r(198);


  • #2
    Ankita:
    I hope that the following toy-example can be helpful:
    Code:
    . set obs 100
    
    . g A=runiform()
    
    . g B=runiform()
    
    . g C=runiform()
    
    . foreach var of varlist A-C {
      2. generate gr`var'=`var'
      3. }
    Besides:
    - I will move all the replacements outside the loop;
    - please note that after -if - expression qualifier, two equal signs are needed.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3

      Carlo gives good advice. I guess you want something more like

      Code:
      foreach  v of varlist var G6-g162{
           generate gr`v' = 0 if `v' == 0 | `v' == 1 
           replace gr`v' = 0.02 if `v' == 2
           replace gr`v' = 0.07 if `v' ==3
      }
      1. The foreach statement names a local macro to be used within the loop.

      2. When testing for equality you need to use == not =.

      3. Your syntax gr(k) looks like pure guesswork or some other language's syntax.

      4. It is impossible for a value to be both 0 and 1. I guess you want 0 or 1. Note that

      Code:
      if gr== 0 & 1
      would be legal in some circumstances, but it is not what you want, as Stata parses that as if (gr == 0) & 1 and 1 is always true. It doesn't interpret it as if (gr == 0) & (gr == 1) which as said will never be true.

      5. The statement starting

      Code:
      generate gr =
      would even if made legal fail second time around the loop as the variable gr then already exists.

      6. Braces or curly brackets must pair { } and be placed properly.

      Comment


      • #4
        I want to change the content of my string variables with the conditions for example i have variable veg and has got values 1-7 but i want to change these values from 1 2 3 4 5 6 7 to 0.02, 0.07 ,0.14 ,0.43, 0.79, 1, 2.5, 4 am trying to use command foreach and generate and replace all together but getting errors 612,198 101
        kindly help me with the logic and syntax or is there any other method to do this

        Comment


        • #5

          . foreach var of varlist g6-g161 g268 g368 g468{
          2.
          . generate gr`v'=`var'
          3.
          . }
          (124 missing values generated)
          variable gr already defined
          r(110);

          . replace gr`v'=0 if `v'== 0 | `v'== 1
          ==0 invalid name
          r(198);

          .
          . replace gr`v'=0.02 if `v'== 2
          ==2 invalid name
          r(198);

          .
          . replace gr`v'=0.07 if `v'== 3
          ==3 invalid name
          r(198);

          this a kind of output am getting without any real changes
          please suggest what is wrong
          i actually have original codes in SAS software but am trying to update my tool in STATA14

          Comment


          • #6
            Ankita:
            ouside the loop, you cannot use the same notation you typed inside the loop.
            You probably want to -replace- the content of your string.
            Hence, you may want something like:
            Code:
            replace <yourstring>="0" if <yourstring>=="2"
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Following Carlo's hint, you may want something more like


              Code:
              foreach v of varlist g6-g161 g268 g368 g468 { 
                  generate gr`v' = `v'
                  replace gr`v' = "0" if `v'== "0" | `v'== "1" 
                  replace gr`v' = "0.02" if `v'== "2"
              }
              On the other hand, you're likely to be better off with numeric variables and value labels.

              Comment


              • #8
                Thanks for input Carlos and nick
                but few of my variables are string and they don't have values and that's why now am getting type mismatch error 109
                mid I drop those string variable and run the foreach command it should work right???

                Comment


                • #9
                  Ankita:
                  I fail to follow your last post.
                  As usual, one of the best approaches to get helpful replies is providing the list with a small example/excerpt of your dataset (type -search dataex-, install it before using).
                  Kind regards,
                  Carlo
                  (Stata 19.0)

                  Comment


                  • #10
                    You told us that your problem was to do with string variables in #4 and advice followed accordingly.

                    But you had advice already in #7 about numeric variables with value labels being a better idea.

                    Also, you're complaining in #8 that code you don't show us doesn't work. We can't easily explain why.

                    Comment


                    • #11
                      foreach v of varlist g6-g17{
                      2.
                      . generate gr`v' = `v'
                      3. replace gr`v'= "0" if `v'== "0" | `v'== "1"
                      4. replace gr`v'= "0.02" if `v'== "2"
                      5. replace gr`v'= "0.07" if `v' == "3"
                      6. replace gr`v'= "1.4" if `v'== "4"
                      7. }

                      so following the advice i tried this command just for a sample dataset and numeric variable only from g6-g17
                      and it went through but when i checked my dataset for changes it showed me the new generated variable as 'gr' and the values dint reflect they should be in command.

                      so my intent is to change the values in my dataset from 0 1 2 3 4 5 6 7 8 9 into decimals as 0 0.02 0.07 1.4 and so but i think am doing something wrong to get the output, I can not provide sample data due to ethical issues.
                      thanks and regards
                      Ankita


                      Comment


                      • #12
                        My guess is that you are running the code line-by-line from a do-file editor window. If you do that the local definition is not visible. Local means within the locality, and "locality" means here "within the same block of code being executed".

                        Run the code all at once to solve this.

                        Comment


                        • #13
                          yup i was running the codes from domfile , so now i copy pasted whole set of commands into STATA and execute the commands but now am getting an error 109" type match"
                          so I notice that all my variables in dataset are in "byte storage" and I need to change them to "float" to run foreach loop
                          foreach v of varlist g11-g17{
                          2.
                          . generate gr`v'= `v'
                          3.
                          . replace gr`v'= "4" if `v'== "9"
                          4.
                          . replace gr`v'= "2.5" if `v'== "8"
                          5.
                          . replace gr`v'= "1" if `v'== "7"
                          6.
                          . replace gr`v'= "0.79" if `v'== "6"
                          7.
                          . replace gr`v'= 0.43 if `v'== "5"
                          8.
                          . replace gr`v'= 0.14 if `v'== "4"
                          9.
                          . replace gr`v'= 0.07 if `v'== "3"
                          10.
                          . replace gr`v'= 0.02 if `v'== "2"
                          11.
                          . replace gr`v'= 0 if `v'== "1"
                          12.
                          . }
                          (115 missing values generated)
                          type mismatch
                          r(109);

                          . desc g11-g17

                          storage display value
                          variable name type format label variable label
                          --------------------------------------------------------------------------------
                          g11 byte %10.0g g11
                          g12 byte %10.0g g12
                          g13 byte %10.0g g13
                          g14 byte %10.0g g14
                          g15 byte %10.0g g15
                          g16 byte %10.0g g16
                          g17 byte %10.0g g17

                          . recast float g11-g17 [, force]
                          weights not allowed
                          r(101);

                          . recast float [, force]
                          varlist required
                          r(100);

                          . recast float varlist [, force]
                          variable varlist not found
                          r(111);

                          . recast float varlist of var g11-g17 [, force]
                          variable varlist not found
                          r(111);

                          . recast float g [, force]
                          g ambiguous abbreviation
                          r(111);

                          . recast float g11 [, force]
                          weights not allowed
                          r(101);

                          any suggestions to do this
                          regards
                          Ankita

                          Comment


                          • #14
                            You're floundering wildly through not understanding basics at all. I'd seriously suggest that you may need to go back and read [GS] and the first chapters of [U]. Or pay someone who knows more Stata locally to do your coding.

                            The g* variables are numeric and some of your code is appropriate if and only if they are string. Your code isn't even consistent on that.

                            The comment in #10 remains valid:

                            You told us that your problem was to do with string variables in #4 and advice followed accordingly.

                            But you had advice already in #7 about numeric variables with value labels being a better idea.
                            This may be what you need.

                            Code:
                            label def mylabel 1 "0" 2 "0.02" 3 "0.07" 4 "0.14" 5 "0.43" 6 "0.79" 7 "1" 8 "2.5" 9 "4"
                            
                            foreach v of varlist g11-g17 {
                                label val `v' mylabel
                            }


                            Or this.

                            Code:
                            foreach v of varlist g11-g17 {
                                gen gr`v' = .
                                forval j = 1/9 {
                                    local which : word `j' of 0 0.02 0.07 0.14 0.43 0.79 1 2.5 4
                                    replace gr`v' = `which' if `v' == `j'
                                }
                            }

                            I can't be sure, as I don't know what you will do with the results.






                            Last edited by Nick Cox; 19 May 2017, 03:28.

                            Comment

                            Working...
                            X