Announcement

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

  • Getting Tokenize to shift within Macros

    I have 3 macros full of ticker symbols.

    stock1
    stock2
    stock3

    Each relates to a region region 1, 2 and 3 respectively.

    I want to run the stocks through the macro in order of the stock with the highest predicted returns. When I run this through I want to drop the correct region for each share into file name below to collect the files from the correct places.

    This will mean my output is aligned to my input.

    I have the following dataset with code: -

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 StockCode float(p_Model1 site)
    "3A2"   16.046179 3
    "AZZVF" 16.046179 1
    "R8G"   10.384218 3
    "R8G"   10.384218 3
    "F1D"    4.277653 3
    "FLNDF"  4.277653 1
    "BCN"   1.7931292 3
    "BCRMF" 1.7931292 1
    "EEYMF"   1.25635 1
    "HFSRF" 1.2384268 1
    "MSB"   1.1480607 1
    "GS5"   1.0767547 3
    "GSS"   1.0767547 1
    "6G4"   1.0551568 3
    "5AM"   1.0237685 3
    "PXPEF"   .898391 1
    "NGVT"   .7022493 1
    "OEC"    .6866636 1
    "OEC"    .6866636 1
    "VRS"    .6375682 3
    "BNB"    .6205041 3
    "BYRSF"  .6205041 1
    "MF6X"   .6187902 3
    "BKY"   .58502084 3
    "GRA"   .58000416 1
    "CC"    .55615383 1
    "CC"    .55615383 1
    "KOP"   .55576825 1
    "4B8"   .54208446 3
    "KAZ"    .4982034 3
    "AVY"    .4978487 1
    "ARCH"   .4870843 1
    "SCPA"   .4794053 3
    "CRPR"   .4788359 3
    "CRDA"   .4755772 3
    "SMG"    .4718292 1
    "FOE"    .4664374 1
    "AXTA"  .46148485 1
    "NEU"     .454123 1
    "CCF"    .4478839 1
    "CCF"    .4478839 1
    "CBM"     .445943 1
    "CBM"     .445943 1
    "FXPO"    .445387 3
    "FMC"    .4400076 1
    "NL"      .438312 1
    "ASIX"   .4360653 1
    "BASA"   .4299021 3
    "HCC"    .4263625 1
    "FMSA"   .4247648 1
    "IMDZF"  .4230024 1
    "TET"     .422565 3
    "PX"     .4219591 1
    "SCCO"   .4200784 1
    "MMLTF"  .4196371 1
    "IFF"     .419348 1
    "FCX"    .4189484 1
    "FCX"    .4189484 1
    "FPM.B"  .4189484 3
    "FCX"    .4189484 1
    "FPM.B"  .4189484 3
    "FPM.B"  .4189484 3
    "TTTRF"  .4157627 1
    "UNVR"   .4129726 1
    "POL"    .4109377 1
    "SQM"     .410175 1
    "ECL"    .4075709 1
    "RBTEF"  .4065716 1
    "QYM"     .405989 3
    "DTI"    .4053736 3
    "GPL"    .4040587 1
    "GCP"    .4029003 1
    "TSE"   .40277445 1
    "OCI"    .4021893 3
    "OCINF"  .4021893 1
    "DEL"    .4012251 1
    "DEL"    .4012251 1
    "4975"    .401096 2
    "RRS"    .4008736 3
    "GFM"    .4001553 3
    "IFF"    .3999876 3
    "KWR"    .3999051 1
    "DQ"     .3991729 1
    "SYX"    .3986178 3
    "8090"   .3975058 2
    "CDFQF"  .3969396 1
    "CDFQF"  .3969396 1
    "CDFQF"  .3969396 1
    "TSDOF"  .3946417 1
    "SYT"    .3941617 1
    "CE"      .392188 1
    "CE"      .392188 1
    "CE"      .392188 1
    "7637"   .3918008 2
    "OMN"    .3912422 1
    "4611"   .3894786 2
    "4971"   .3884892 2
    "TTNMF"  .3883018 1
    "5727"   .3883018 2
    "UMI"    .3881032 3
    end
    
    
    preserve
    keep if site == 1
    levelsof StockCode, local(stock2)
    restore, preserve
    keep if site == 2
    levelsof StockCode, local(stock2)
    restore, preserve
    keep if site == 3
    levelsof StockCode, local(stock3)
    restore
    levelsof site, local(site1)
    local stock "`stock1'" "`stock2'" "`stock3'"
    local site1 "1 2 3"
    tokenize `"`site1'"'
    
    foreach s of local stock {
    capture noisily {
    
    use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
    
    merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region`1'/`s'.dta", nogenerate
    }
    macro shift
    }
    I'm getting the following error.....

    (Source: Yahoo Finance!)
    file /Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region1/ .dta not found
    (Source: Yahoo Finance!)
    file /Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region2/2670.dta not found
    (Source: Yahoo Finance!)
    file /Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region3/4813.dta not found
    (Source: Yahoo Finance!)
    file /Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region/6087.dta not found
    (Source: Yahoo Finance!)
    file /Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region/6565.dta not found
    (Source: Yahoo Finance!)

    etc etc
    Last edited by Giles German; 22 Jan 2018, 14:50.

  • #2
    I don't quite follow what you are trying to do here. Be that as it may, the error you are encountering arises because your local macro site is initialized to contain "1 2 3" (not including the quotes). So after you -macro shift- 3 times, there is nothing left to shift into and thereafter `1' is just the empty string. When that gets substituted into the name of the file you are trying to -merge-, you have Stata looking for a non-existent file, hence Stata's complaint.

    If I can correctly guess what you want to do, it would be this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 StockCode float(p_Model1 site)
    "3A2"   16.046179 3
    "AZZVF" 16.046179 1
    "R8G"   10.384218 3
    "R8G"   10.384218 3
    "F1D"    4.277653 3
    "FLNDF"  4.277653 1
    "BCN"   1.7931292 3
    "BCRMF" 1.7931292 1
    "EEYMF"   1.25635 1
    "HFSRF" 1.2384268 1
    "MSB"   1.1480607 1
    "GS5"   1.0767547 3
    "GSS"   1.0767547 1
    "6G4"   1.0551568 3
    "5AM"   1.0237685 3
    "PXPEF"   .898391 1
    "NGVT"   .7022493 1
    "OEC"    .6866636 1
    "OEC"    .6866636 1
    "VRS"    .6375682 3
    "BNB"    .6205041 3
    "BYRSF"  .6205041 1
    "MF6X"   .6187902 3
    "BKY"   .58502084 3
    "GRA"   .58000416 1
    "CC"    .55615383 1
    "CC"    .55615383 1
    "KOP"   .55576825 1
    "4B8"   .54208446 3
    "KAZ"    .4982034 3
    "AVY"    .4978487 1
    "ARCH"   .4870843 1
    "SCPA"   .4794053 3
    "CRPR"   .4788359 3
    "CRDA"   .4755772 3
    "SMG"    .4718292 1
    "FOE"    .4664374 1
    "AXTA"  .46148485 1
    "NEU"     .454123 1
    "CCF"    .4478839 1
    "CCF"    .4478839 1
    "CBM"     .445943 1
    "CBM"     .445943 1
    "FXPO"    .445387 3
    "FMC"    .4400076 1
    "NL"      .438312 1
    "ASIX"   .4360653 1
    "BASA"   .4299021 3
    "HCC"    .4263625 1
    "FMSA"   .4247648 1
    "IMDZF"  .4230024 1
    "TET"     .422565 3
    "PX"     .4219591 1
    "SCCO"   .4200784 1
    "MMLTF"  .4196371 1
    "IFF"     .419348 1
    "FCX"    .4189484 1
    "FCX"    .4189484 1
    "FPM.B"  .4189484 3
    "FCX"    .4189484 1
    "FPM.B"  .4189484 3
    "FPM.B"  .4189484 3
    "TTTRF"  .4157627 1
    "UNVR"   .4129726 1
    "POL"    .4109377 1
    "SQM"     .410175 1
    "ECL"    .4075709 1
    "RBTEF"  .4065716 1
    "QYM"     .405989 3
    "DTI"    .4053736 3
    "GPL"    .4040587 1
    "GCP"    .4029003 1
    "TSE"   .40277445 1
    "OCI"    .4021893 3
    "OCINF"  .4021893 1
    "DEL"    .4012251 1
    "DEL"    .4012251 1
    "4975"    .401096 2
    "RRS"    .4008736 3
    "GFM"    .4001553 3
    "IFF"    .3999876 3
    "KWR"    .3999051 1
    "DQ"     .3991729 1
    "SYX"    .3986178 3
    "8090"   .3975058 2
    "CDFQF"  .3969396 1
    "CDFQF"  .3969396 1
    "CDFQF"  .3969396 1
    "TSDOF"  .3946417 1
    "SYT"    .3941617 1
    "CE"      .392188 1
    "CE"      .392188 1
    "CE"      .392188 1
    "7637"   .3918008 2
    "OMN"    .3912422 1
    "4611"   .3894786 2
    "4971"   .3884892 2
    "TTNMF"  .3883018 1
    "5727"   .3883018 2
    "UMI"    .3881032 3
    end
    
    levelsof site, local(sites)
    foreach i of local sites {
        levelsof StockCode if site == `i', local(stock`i')
    }
    
    
    foreach i of local sites {
        foreach s of local stock`i' {
            capture noisily {
                use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
    
                merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region`i'/`s'.dta", nogenerate
            }
        }
    }
    Notes: I have made additional simplifications to your code. The local macro -site1- can be generated once (and I renamed it sites to avoid any confusion by the name's suggestion that it has something to do specifically with site 1) and then used to index a -foreach- loop that generates the various stock# local macros. The combined macro stock is not needed, nor is -tokenize-.

    Comment


    • #3
      Starting with your sample data and running this modified version of your code (and do not overlook the initial typo corrected in lines 3/4)
      Code:
      preserve
      keep if site == 1
      // levelsof StockCode, local(stock2)
      levelsof StockCode, local(stock1)
      restore, preserve
      keep if site == 2
      levelsof StockCode, local(stock2)
      restore, preserve
      keep if site == 3
      levelsof StockCode, local(stock3)
      restore
      levelsof site, local(site1)
      local stock "`stock1'" "`stock2'" "`stock3'"
      local site1 "1 2 3"
      tokenize `"`site1'"'
      
      foreach s of local stock {
      
      capture noisily {
      macro list _1 _s
      // use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
      
      // merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region`1'/`s'.dta", nogenerate
      }
      macro shift
      }
      eventually shows the following results
      Code:
      _1:             1
      _s:             ARCH
      _1:             2
      _s:             ASIX
      _1:             3
      _s:             AVY
      local macro `1' not found
      local macro `1' not found
      local macro `1' not found
      local macro `1' not found
      ... for many more iterations
      Each time you are incrementing to a new stock, you are also shifting in a new token. (This was also clear from the output you showed.) Perhaps something like the following would help.
      Code:
      forvalues t=1/3 {
      foreach s of local stock {
      capture noisily {
      
      use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
      
      merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector1/Region`t'/`s'.dta", nogenerate
      }
      }
      }
      Added in edit: Clyde's solution is more comprehensive. My objective was limited to explaining what you had done wrong, and illustrating a possible solution.
      Last edited by William Lisowski; 22 Jan 2018, 15:33.

      Comment


      • #4
        Thanks for this. Is it possible to run the shares through the foreach cycle in order of the stockcode with the highest p model as orders in the data set above?

        Comment


        • #5
          Sort on the model criterion and then loop over observation indexes.

          In fact they are already sorted, so your loop presumably starts something like

          Code:
          count 
          
          forval j = 1/`r(N)' {
          and uses the data in each observation so indexed.

          Comment


          • #6
            For the variable site I have a problem. This is how I created the variable.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str9 StockCode float(region site) str10 exch str14 Country
            "VNP"      1 0 ""         "Canada"        
            "EMB"      3 3 "EMB.ST"   "Germany"       
            "5142"     3 2 ""         "Japan"         
            "4401"     3 2 ""         "Japan"         
            "ASIX"     3 1 ""         "United States" 
            "AFAGR"    3 3 "AFAGR.HE" "Finland"       
            "V5W"      3 3 "V5W.F"    "Germany"       
            "AFRK"     3 3 "AFRK.L"   "United Kingdom"
            "AEM"      3 1 ""         "United States" 
            "AEM"      1 0 ""         "Canada"        
            "4955"     3 2 ""         "Japan"         
            "AM1"      3 3 "AM1.HE"   "Finland"       
            "MUNK1EUR" 3 0 ""         "Sweden"        
            "AGI"      1 0 ""         "Canada"        
            "ALC"      3 0 ""         "Poland"        
            "AA"       3 1 ""         "United States" 
            "AXR"      1 0 ""         "Canada"        
            "AXU"      3 1 ""         "United States" 
            "ALO"      1 0 ""         "Canada"        
            "ALS"      1 0 ""         "Canada"        
            "AJM"      2 0 ""         "Australia"     
            "AWC"      2 0 ""         "Australia"     
            "ARG"      1 0 ""         "Canada"        
            "AP"       3 1 ""         "United States" 
            "347"      3 0 ""         "Hong Kong"     
            "1104"     3 0 ""         "Hong Kong"     
            "APAM"     3 3 "APAM.AS"  "Netherlands"   
            "APMSF"    3 1 ""         "United States" 
            "ARCH"     3 1 ""         "United States" 
            "5208"     3 2 ""         "Japan"         
            "AZ"       1 0 ""         "Canada"        
            "3313"     3 0 ""         "Hong Kong"     
            "5857"     3 2 ""         "Japan"         
            "A19"      3 3 "A19.BE"   "Germany"       
            "4216"     3 2 ""         "Japan"         
            "AKG"      1 0 ""         "Canada"        
            "AKG"      3 1 ""         "United States" 
            "AOT"      1 0 ""         "Canada"        
            "ASH"      3 1 ""         "United States" 
            "ATYM"     3 3 "ATYM.L"   "United Kingdom"
            "AGO"      2 0 ""         "Australia"     
            "AUZ"      2 0 ""         "Australia"     
            "AVTX"     3 3 "AVTX.AS"  "Netherlands"   
            "AVZ"      2 0 ""         "Australia"     
            "3A2"      3 3 "3A2.BE"   "Germany"       
            "AZZVF"    3 1 ""         "United States" 
            "BCN"      1 0 ""         "Canada"        
            "BCRMF"    3 1 ""         "United States" 
            "BCN"      3 3 "BCN.L"    "United Kingdom"
            "5195"     3 2 ""         "Japan"         
            "BGM"      1 0 ""         "Canada"        
            "BSE"      2 0 ""         "Australia"     
            "BSRUF"    3 1 ""         "United States" 
            "BSE"      3 3 "BSE.L"    "United Kingdom"
            "BDR"      2 0 ""         "Australia"     
            "BCM"      1 0 ""         "Canada"        
            "1329"     3 0 ""         "Hong Kong"     
            "BKY"      2 0 ""         "Australia"     
            "BKY"      3 3 "BKY.L"    "United Kingdom"
            "BIG"      2 0 ""         "Australia"     
            "R8G"      3 3 "R8G.BE"   "Germany"       
            "BIG"      2 0 ""         "Australia"     
            "R8G"      3 3 "R8G.BE"   "Germany"       
            "BGS"      2 0 ""         "Australia"     
            "EEYMF"    3 1 ""         "United States" 
            "JAY"      3 3 "JAY.L"    "United Kingdom"
            "BSL"      2 0 ""         "Australia"     
            "JAY"      3 3 "JAY.L"    "United Kingdom"
            "BSL"      2 0 ""         "Australia"     
            "BIP.UN"   1 0 ""         "Canada"        
            "BIP"      3 1 ""         "United States" 
            "BUR"      3 3 "BUR.BE"   "Germany"       
            "24"       3 0 ""         "Hong Kong"     
            "BUR"      3 3 "BUR.BE"   "Germany"       
            "24"       3 0 ""         "Hong Kong"     
            "CBM"      3 1 ""         "United States" 
            "CCJ"      3 1 ""         "United States" 
            "CCO"      1 0 ""         "Canada"        
            "CBM"      3 1 ""         "United States" 
            "CCJ"      3 1 ""         "United States" 
            "CCO"      1 0 ""         "Canada"        
            "CS"       1 0 ""         "Canada"        
            "CDV"      2 0 ""         "Australia"     
            "CRDNF"    3 1 ""         "United States" 
            "CDV"      2 0 ""         "Australia"     
            "CRDNF"    3 1 ""         "United States" 
            "CSI"      3 3 "CSI.BE"   "Germany"       
            "CF"       3 1 ""         "United States" 
            "CPGN"     3 0 ""         "Switzerland"   
            "CIA"      1 0 ""         "Canada"        
            "CIA"      2 0 ""         "Australia"     
            "CCF"      3 1 ""         "United States" 
            "CCF"      3 1 ""         "United States" 
            "3983"     3 0 ""         "Hong Kong"     
            "H1Q"      3 3 "H1Q.BE"   "Germany"       
            "CGG"      1 0 ""         "Canada"        
            "2099"     3 0 ""         "Hong Kong"     
            "1197"     3 0 ""         "Hong Kong"     
            "CGG"      1 0 ""         "Canada"        
            "2099"     3 0 ""         "Hong Kong"     
            end
            
            generate site = 0
            replace site = 1 if inlist(Country,"United States")
            replace site = 2 if inlist(Country, "Japan")
            replace site = 3 if inlist(Country, "Austria", "Belgium", "Finland") 
            replace site = 3 if inlist(Country,"France", "Germany", "Italy", "Netherlands", "Portugal", "Spain", "United Kingdom")
            I am using site to bring across the regions where I want to do analysis across these specific countries. I have made this numeric so that it will work in the macros. When creating the variable I put "0" in where I did not want the region to be analysed as I couldn't get Stata top put a blank in for numeric variables. With this new code it keeps looking in folder region0 which does not exist. How is best to setup this dataset?

            Comment


            • #7
              When creating the variable I put "0" in where I did not want the region to be analysed as I couldn't get Stata top put a blank in for numeric variables.
              But you can have Stata put in a missing value:

              Code:
              generate site = .
              replace site = 1 if inlist(Country,"United States")
              replace site = 2 if inlist(Country, "Japan")
              replace site = 3 if inlist(Country, "Austria", "Belgium", "Finland")
              replace site = 3 if inlist(Country,"France", "Germany", "Italy", "Netherlands", "Portugal", "Spain", "United Kingdom")
              When you then do -levelsof site-, it will report 1, 2, and 3, but nothing for the missing values, and your loop won't try to find folder region0.

              Do read -help missing- and the Missing Values section of the PDF documentation linked there.

              Note: In case it's not typographically clear, the character for system missing value in the first line of code above is called a period (US) or full stop (UK). On the US keyboard it is in the bottom row, just to the left of the ?/ key.

              Comment


              • #8
                Thanks Clyde that has done the trick. Been scratching my head about that one for a while.

                For the ordered macros i have reviewed the following....

                Originally posted by Nick Cox View Post
                Sort on the model criterion and then loop over observation indexes.

                In fact they are already sorted, so your loop presumably starts something like

                Code:
                count
                
                forval j = 1/`r(N)' {
                and uses the data in each observation so indexed.
                I have used Clyde’s code is this compatible with that code as well? It looks very different i can't see where the two codes link.
                Last edited by Giles German; 23 Jan 2018, 12:41.

                Comment


                • #9
                  I glanced at #3 and made a suggestion answering #4. Sorry, but I can't follow all the details here.

                  Comment


                  • #10
                    In response to #8, I saw, but did not understand
                    Is it possible to run the shares through the foreach cycle in order of the stockcode with the highest p model as orders in the data set above?
                    I don't know what "highest p model" refers to, and I'm not even 100% certain what "shares" means, though I would guess it's a synonym for stockcode. Similarly, I saw but did not understand Nick Cox's reply.

                    So I can't tell whether it's compatible with my approach or not.

                    Also, at least in the code you've shown, your loop is just merging a bunch of files together. Depending on what's in those files, it may make no difference what order you do that in. (The order of the variables may differ, but you can always change that at the end using the -order- command.)

                    Comment


                    • #11
                      Clyde: In #2 there is a variable model_p1 which presumably is connected with the "highest p model".

                      Comment


                      • #12
                        Okay I can elaborate more the p model variable is the prediction from the first regression analysis. There are two models I want to run model 1 export the results into excel (all the stockcodes) and order starting with the highest prediction as per the dataset in post 1. I then want to run model 2 which is analysis against each individual stockcode and then putexcel the results into excel so they line up on the same spreadsheet with model 1 (i.e. in the same order). I've used the capture noisily option so where there is nothing found I want to just drop the ticker in so they line up and move down to the next one leaving the results blank. This is why I want to order the macro starting with the highest prediction or the results will not line up in excel.

                        Comment


                        • #13
                          Thanks, Nick & Giles for explaining the model p thing. So it's first a matter of generating the stocks local macro in order. Using the data set in #2, which has the p_Model1 variable, you can do it this way:

                          Code:
                          gsort -p_Model1
                          count
                          local nobs `r(N)'
                          local stocks
                          forvalues i = 1/`nobs' {
                              local stocks `stocks'  `=p_Model1[`i']'
                          }
                          Note: if, as shown in #2 and commented on by Nick the data are actually already sorted no p_Model, biggest first, then there is no need to do the -gsort- command.

                          Now, this creates a new problem, because the sites are no longer going to be in order. So instead of two loops, it becomes one loop over local macro stocks, and inside the loop you have to grab the appropriate value of site:

                          Code:
                          foreach s of local stocks {
                              summ site if StockCode == `"`s'"', meanonly
                              local site `r(mean)'
                              use some_file, clear
                              merge 1:1... using pathname_involving_`site'/filename_involving_`s'
                              // maybe do other stuff
                          }

                          Comment


                          • #14
                            Thanks Clyde that's great I will take a look to incorporate that into the bigger code in a moment.

                            Back to the original issue I still haven't managed to get this to work. I have showed the problem as follows: -

                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input str87 Stock str14 Country double MarketCap1 str9 StockCode str6 exchange float(region site) str8 add str10 exch long Sector1
                            "Achilles Corp 5142:TYO"                     "Japan"                      275020 "5142"  "TYO" 3 2 "j" ""         1
                            "Afarak Group Plc AFAGR:HEX"                 "Finland"                    207800 "AFAGR" "HEX" 3 3 "e" "AFAGR.HE" 1
                            "Afarak Group Plc V5W:FRA"                   "Germany"                    207800 "V5W"   "FRA" 3 3 "e" "V5W.F"    1
                            "Afarak Group Plc AFRK:LSE"                  "United Kingdom"             207800 "AFRK"  "LSE" 3 3 "e" "AFRK.L"   1
                            "Agro-Kanesho Co Ltd 4955:TYO"               "Japan"                      197570 "4955"  "TYO" 3 2 "j" ""         1
                            "Alamos Gold Inc AGI:TOR"                    "Canada"                    1740000 "AGI"   "TOR" 1 . ""  ""         1
                            "Alchemia SA ALC:WSE"                        "Poland"                     183850 "ALC"   "WSE" 3 . ""  ""         1
                            "Alcoa Corp AA:NYQ"                          "United States"             7650000 "AA"    "NYQ" 3 1 ""  ""         1
                            "Alexco Resource Corp AXR:TOR"               "Canada"                     118890 "AXR"   "TOR" 1 . ""  ""         1
                            "Alexco Resource Corp AXU:ASQ"               "United States"              118910 "AXU"   "ASQ" 3 1 ""  ""         1
                            "Alio Gold Inc ALO:TOR"                      "Canada"                     118340 "ALO"   "TOR" 1 . ""  ""         1
                            "Altius Minerals Corp ALS:TOR"               "Canada"                     386350 "ALS"   "TOR" 1 . ""  ""         1
                            "Alumina Ltd AWC:ASX"                        "Australia"                 4270000 "AWC"   "ASX" 2 . ""  ""         1
                            "APAC Resources Ltd 1104:HKG"                "Hong Kong"                  121530 "1104"  "HKG" 3 . ""  ""         1
                            "Aperam SA APAM:AEX"                         "Netherlands"               3430000 "APAM"  "AEX" 3 3 "e" "APAM.AS"  1
                            "Aperam SA APMSF:PNK"                        "United States"             3430000 "APMSF" "PNK" 3 1 ""  ""         1
                            "Arizona Mining Inc AZ:TOR"                  "Canada"                     766750 "AZ"    "TOR" 1 . ""  ""         1
                            "Artgo Holdings Ltd 3313:HKG"                "Hong Kong"                  177790 "3313"  "HKG" 3 . ""  ""         1
                            "Asahi Yukizai Corp 4216:TYO"                "Japan"                      283270 "4216"  "TYO" 3 2 "j" ""         1
                            "Ascot Resources Ltd AOT:CVE"                "Canada"                     115810 "AOT"   "CVE" 1 . ""  ""         1
                            "Atalaya Mining PLC ATYM:LSE"                "United Kingdom"             245850 "ATYM"  "LSE" 3 3 "e" "ATYM.L"   1
                            "Australian Mines Ltd AUZ:ASX"               "Australia"                  208340 "AUZ"   "ASX" 2 . ""  ""         1
                            "Avantium Holding BV AVTX:AEX"               "Netherlands"                171290 "AVTX"  "AEX" 3 3 "e" "AVTX.AS"  1
                            "Avz Minerals Ltd AVZ:ASX"                   "Australia"                  345380 "AVZ"   "ASX" 2 . ""  ""         1
                            "Avz Minerals Ltd 3A2:BER"                   "Germany"                    345380 "3A2"   "BER" 3 3 "e" "3A2.BE"   1
                            "Avz Minerals Ltd AZZVF:PNK"                 "United States"              345380 "AZZVF" "PNK" 3 1 ""  ""         1
                            "Bacanora Minerals Ltd BCN:CVE"              "Canada"                     168020 "BCN"   "CVE" 1 . ""  ""         1
                            "Bacanora Minerals Ltd BCRMF:PNK"            "United States"              168020 "BCRMF" "PNK" 3 1 ""  ""         1
                            "Bacanora Minerals Ltd BCN:LSE"              "United Kingdom"             168020 "BCN"   "LSE" 3 3 "e" "BCN.L"    1
                            "Barkerville Gold Mines Ltd BGM:CVE"         "Canada"                     188400 "BGM"   "CVE" 1 . ""  ""         1
                            "Beadell Resources Ltd BDR:ASX"              "Australia"                  124400 "BDR"   "ASX" 2 . ""  ""         1
                            "Bear Creek Mining Corp BCM:CVE"             "Canada"                     120550 "BCM"   "CVE" 1 . ""  ""         1
                            "Berkeley Energia Ltd BKY:ASX"               "Australia"                  142290 "BKY"   "ASX" 2 . ""  ""         1
                            "Berkeley Energia Ltd BKY:LSE"               "United Kingdom"             142290 "BKY"   "LSE" 3 3 "e" "BKY.L"    1
                            "Big Un Ltd BIG:ASX"                         "Australia"                  301290 "BIG"   "ASX" 2 . ""  ""         1
                            "Big Un Ltd R8G:BER"                         "Germany"                    301290 "R8G"   "BER" 3 3 "e" "R8G.BE"   1
                            "Big Un Ltd BIG:ASX"                         "Australia"                  301290 "BIG"   "ASX" 2 . ""  ""         1
                            "Big Un Ltd R8G:BER"                         "Germany"                    301290 "R8G"   "BER" 3 3 "e" "R8G.BE"   1
                            "Birimian Ltd BGS:ASX"                       "Australia"                  103180 "BGS"   "ASX" 2 . ""  ""         1
                            "Birimian Ltd EEYMF:PNK"                     "United States"              103180 "EEYMF" "PNK" 3 1 ""  ""         1
                            "Bluejay Mining PLC JAY:LSE"                 "United Kingdom"             169700 "JAY"   "LSE" 3 3 "e" "JAY.L"    1
                            "BlueScope Steel Ltd BSL:ASX"                "Australia"                 5080000 "BSL"   "ASX" 2 . ""  ""         1
                            "Bluejay Mining PLC JAY:LSE"                 "United Kingdom"             169700 "JAY"   "LSE" 3 3 "e" "JAY.L"    1
                            "BlueScope Steel Ltd BSL:ASX"                "Australia"                 5080000 "BSL"   "ASX" 2 . ""  ""         1
                            "Bumi Resources Minerals Tbk PT BUR:BER"     "Germany"                    282150 "BUR"   "BER" 3 3 "e" "BUR.BE"   1
                            "Bumi Resources Minerals Tbk PT BUR:BER"     "Germany"                    282150 "BUR"   "BER" 3 3 "e" "BUR.BE"   1
                            "Cardinal Resources Ltd CDV:ASX"             "Australia"                  104870 "CDV"   "ASX" 2 . ""  ""         1
                            "Cardinal Resources Ltd CRDNF:PNK"           "United States"              104870 "CRDNF" "PNK" 3 1 ""  ""         1
                            "Cardinal Resources Ltd CDV:ASX"             "Australia"                  104870 "CDV"   "ASX" 2 . ""  ""         1
                            "Cardinal Resources Ltd CRDNF:PNK"           "United States"              104870 "CRDNF" "PNK" 3 1 ""  ""         1
                            "China BlueChemical Ltd 3983:HKG"            "Hong Kong"                 1150000 "3983"  "HKG" 3 . ""  ""         1
                            "China BlueChemical Ltd H1Q:BER"             "Germany"                   1150000 "H1Q"   "BER" 3 3 "e" "H1Q.BE"   1
                            "China Hengshi Foundation Co Ltd 1197:HKG"   "Hong Kong"                  208570 "1197"  "HKG" 3 . ""  ""         1
                            "China Hengshi Foundation Co Ltd 1197:HKG"   "Hong Kong"                  208570 "1197"  "HKG" 3 . ""  ""         1
                            "China Hengshi Foundation Co Ltd 1197:HKG"   "Hong Kong"                  208570 "1197"  "HKG" 3 . ""  ""         1
                            "China Mining Resources Group Ltd 340:HKG"   "Hong Kong"                  195840 "340"   "HKG" 3 . ""  ""         1
                            "Compania de Minas Buenaventura SAA MBU:FRA" "Germany"                   2870000 "MBU"   "FRA" 3 3 "e" "MBU.F"    1
                            "Compania de Minas Buenaventura SAA BVN:NYQ" "United States"             2870000 "BVN"   "NYQ" 3 1 ""  ""         1
                            "Compania de Minas Buenaventura SAA MBU:FRA" "Germany"                   2870000 "MBU"   "FRA" 3 3 "e" "MBU.F"    1
                            "Compania de Minas Buenaventura SAA BVN:NYQ" "United States"             2870000 "BVN"   "NYQ" 3 1 ""  ""         1
                            "Compania de Minas Buenaventura SAA MBU:FRA" "Germany"                   2870000 "MBU"   "FRA" 3 3 "e" "MBU.F"    1
                            "Compania de Minas Buenaventura SAA BVN:NYQ" "United States"             2870000 "BVN"   "NYQ" 3 1 ""  ""         1
                            "Continental Gold Inc CNL:TOR"               "Canada"                     435800 "CNL"   "TOR" 1 . ""  ""         1
                            "Critical Elements Corp CRE:CVE"             "Canada"                     137770 "CRE"   "CVE" 1 . ""  ""         1
                            "Dacian Gold Ltd DCN:ASX"                    "Australia"                  355900 "DCN"   "ASX" 2 . ""  ""         1
                            "Dalradian Resources Inc DLR:FRA"            "Germany"                    251350 "DLR"   "FRA" 3 3 "e" "DLR.F"    1
                            "Dalradian Resources Inc DALR:LSE"           "United Kingdom"             251350 "DALR"  "LSE" 3 3 "e" "DALR.L"   1
                            "Dottikon Es Holding AG DESN:SWX"            "Switzerland"                756070 "DESN"  "SWX" 3 . ""  ""         1
                            "Dottikon Es Holding AG DTI:BER"             "Germany"                    756070 "DTI"   "BER" 3 3 "e" "DTI.BE"   1
                            "Dundee Precious Metals Inc DPM:TOR"         "Canada"                     344340 "DPM"   "TOR" 1 . ""  ""         1
                            "Dundee Precious Metals Inc DPM:TOR"         "Canada"                     344340 "DPM"   "TOR" 1 . ""  ""         1
                            "Ecobalt Solutions Inc ECS:TOR"              "Canada"                     140840 "ECS"   "TOR" 1 . ""  ""         1
                            "Energy Resources Of Australia Ltd ERA:ASX"  "Australia"                  278980 "ERA"   "ASX" 2 . ""  ""         1
                            "Energy Resources Of Australia Ltd ERA:ASX"  "Australia"                  278980 "ERA"   "ASX" 2 . ""  ""         1
                            "First Cobalt Corp FCC:ASX"                  "Australia"                  142650 "FCC"   "ASX" 2 . ""  ""         1
                            "First Cobalt Corp FCC:ASX"                  "Australia"                  142650 "FCC"   "ASX" 2 . ""  ""         1
                            "Fission Uranium Corp FCU:TOR"               "Canada"                     243990 "FCU"   "TOR" 1 . ""  ""         1
                            "Flat Glass Group Co Ltd 6865:HKG"           "Hong Kong"                  360340 "6865"  "HKG" 3 . ""  ""         1
                            "Flinders Mines Ltd FMS:ASX"                 "Australia"                  184340 "FMS"   "ASX" 2 . ""  ""         1
                            "Flinders Mines Ltd F1D:BER"                 "Germany"                    184340 "F1D"   "BER" 3 3 "e" "F1D.BE"   1
                            "Flinders Mines Ltd FLNDF:PNK"               "United States"              184340 "FLNDF" "PNK" 3 1 ""  ""         1
                            "Fluence Corporation Ltd FLC:ASX"            "Australia"                  116140 "FLC"   "ASX" 2 . ""  ""         1
                            "Franco-Nevada Corp FNV:TOR"                 "Canada"                   10570000 "FNV"   "TOR" 1 . ""  ""         1
                            "Franco-Nevada Corp. FNNVF:NYQ"              "United States"            10570000 "FNNVF" "NYQ" 3 1 ""  ""         1
                            "Garibaldi Resources Corp GGI:CVE"           "Canada"                     152600 "GGI"   "CVE" 1 . ""  ""         1
                            "Gascoyne Resources Ltd GCY:ASX"             "Australia"      129020.00000000001 "GCY"   "ASX" 2 . ""  ""         1
                            "Gascoyne Resources Ltd 6G4:FRA"             "Germany"        129020.00000000001 "6G4"   "FRA" 3 3 "e" "6G4.F"    1
                            "Gem Diamonds Ltd GEMD:LSE"                  "United Kingdom"             110170 "GEMD"  "LSE" 3 3 "e" "GEMD.L"   1
                            "Global Geoscience Ltd GSC:ASX"              "Australia"                  295520 "GSC"   "ASX" 2 . ""  ""         1
                            "Gold Reserve GRZ:CVE"                       "Canada"                     198010 "GRZ"   "CVE" 1 . ""  ""         1
                            "Gold Road Resources Ltd GOR:ASX"            "Australia"                  351310 "GOR"   "ASX" 2 . ""  ""         1
                            "Gold Standard Ventures DSHRE:OBB"           "United States"              267640 "DSHRE" "OBB" 3 1 ""  ""         1
                            "Gold Standard Ventures Corp GSV:TOR"        "Canada"                     267640 "GSV"   "TOR" 1 . ""  ""         1
                            "Gold Standard Ventures Corp 6AZ:FRA"        "Germany"                    267640 "6AZ"   "FRA" 3 3 "e" "6AZ.F"    1
                            "GoldMoney Inc XAU:TOR"                      "Canada"                     271060 "XAU"   "TOR" 1 . ""  ""         1
                            "Great Panther Silver Ltd GPR:TOR"           "Canada"                     176210 "GPR"   "TOR" 1 . ""  ""         1
                            "Great Panther Silver Ltd GPL:ASQ"           "United States"              176240 "GPL"   "ASQ" 3 1 ""  ""         1
                            "Guyana Goldfields Inc GG3:BER"              "Germany"                    493790 "GG3"   "BER" 3 3 "e" "GG3.BE"   1
                            "Guyana Goldfields Inc GUY:TOR"              "Canada"                     493790 "GUY"   "TOR" 1 . ""  ""         1
                            "Harte Gold Corp HRT:TOR"                    "Canada"                     139930 "HRT"   "TOR" 1 . ""  ""         1
                            end
                            
                            local p 1
                            
                            levelsof site, local(sites)
                            foreach i of local sites {
                                levelsof StockCode if site == `i', local(stock`i')
                            
                            }
                            
                            
                            foreach i of local sites {
                                foreach s of local stock`i' {
                                capture noisily {
                            
                                        use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
                            
                                        merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector`p'/Region`i'/`s'.dta", nogenerate
                            
                            
                            
                            
                            local z `s'
                            
                            regress `z' BNO SLV
                            
                            }
                            }
                            }
                            The reason it is still not working is because when I download a Japanese share they all have numbers for example they might be called "9508". Stata will not allow you to put a number as a variable therefore I have put a "j" in from of the number to make it j9508. For European stocks these all sometimes start with numbers so in front of european numbers I have put an "e". I have therefore added yet another variable to the dates called add (Add-on) which I would like to incorporate before the local z. My proposed code is as follows which doesn't work I think the foreach is wrong: -

                            Code:
                            local p 1
                            
                            foreach i of local sites {
                                levelsof StockCode if site == `i', local(stock`i')
                            }
                            foreach a of local add {
                            levelsof StockCode if add == `"a"', local(stock`a')
                            }
                            
                            foreach a of local add {
                            foreach i of local sites {
                                foreach s of local stock`i' {
                                capture noisily {
                            
                                        use "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/STATA Data 4.dta", replace
                            
                                        merge 1:1 Date using "/Users/gilesgerman/Documents/Stokpix/StokpixModels/Model2/Equities/Sector`p'/Region`i'/`s'.dta", nogenerate
                            
                            
                            
                            
                            local z `a' `s'
                            
                            regress `z' BNO SLV
                            I hope you can help....

                            Comment


                            • #15
                              You're confusing me.

                              You say you have created some variable called add. But then the code in the last panel of #14 refers to a local macro add. Now, there's nothing illegal about having a local macro with the same name as a variable (though it is not good practice). But you never show how it is defined. If it isn't defined somewhere, then your -foreach a of local add- loop will find nothing to loop over and the entire loop will just be skipped.

                              Now, since you also have a line inside that loop that says -levelsof StockCode if add == `"a"', local(stock`a')-, I'm going to speculate that local macro is either actually defined as, or intended to be defined as (you should check), a list of values of the variable add, so it will be either e or j. Then you go on to define -local z `a' `s'-, so that z contains the current value of add under consideration, and then you go to -regress `z' BNO SLV-. Given that defintion, this last command will be interpreted as
                              -regress ? other_stuff- where ? is either e or j. But there are no variables named e or j in your dataset. So I don't know what you are trying to do here, but this command will certainly break your code. Everything that appears in a -regress- command until you reach the comma (if any) has to be the name of a variable name. I should add that it looks like the `s' part of `z' doesn't qualify either--again, the definitions of stock1, stock2, and stock3 are not actually shown here, but assuming they are similar to what you have shown previously in this thread, `s' will contain a specific stock code, not a variable.

                              So, again, while I can't figure out what you're trying to do here, I can see why this code is just throwing errors and producing no results.

                              Comment

                              Working...
                              X