Announcement

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

  • nested forvalues (foreach) and extract successively a string local

    Dear Stata users,

    The question is about using a loop to perform -shp2dta- (SSC) command. I have several .shp files of different provinces and of different levels. Of provinces, they are for example, Qinghai, Shaanxi, Shanghai, Shanxi, Sichuan, Tianjin, Xinjiang, Yunnan, Zhejiang. Of levels, they are state, city, and county. Those .shp files' name are combination of province and level, for example, Qinghai_state.shp, Qinghai_city.shp, Qinghai_county.shp and the likes. The basic -shp2dta- code that transforming .shp files into .dta files is:
    Code:
    shp2dta using shpfilename, data(newfilename1) coor(newfilename2)
    thus for the three files of Qinghai province, the transforming codes should be:
    Code:
    shp2dta using Qinghai_state, data(Qinghaistate_label) coor(Qinghaistate_map)
    shp2dta using Qinghai_city, data(Qinghaicity_label) coor(Qinghaicity_map)
    shp2dta using Qinghai_county, data(Qinghaicounty_label) coor(Qinghaicounty_map)
    As I have so many .shp files of different provinces and levels, I seek to perform this transforming task in a loop. And not surprisingly I failed. Belows is one of my tries. Hope someone can help me.
    Code:
    local province "Qinghai Shaanxi Shanghai Shanxi"
    local maptype "state city county"
    forvalues n = 1/4 {
     forvalues t = 1/3 {
       local pro: word `n' of `province'
       local map: word `t' of `maptype'
       display "`pro'" "_" "`map'"
       shp2dta using "`pro'" "_" "`map'", data("`pro'" "`map'" "_label") coor("`pro'" "`map'" "_map")
       local n = `n'+1
       local t = `t'+1
       }
      }

  • #2
    There is nothing that anyone can test here easily unless they have access to the same files. I would just wonder whether often you need

    Code:
     
     "`pro'_`map'"
    rather than

    Code:
     
     "`pro'" "_" "`map'

    Comment


    • #3
      Hi Nick, I upload some necessary files but forum software alerts me Invalid File or Error Uploading Image. Come back to our story. As far I can receive twofold wrong from Stata message. Firstly the nested forvalues goes wrong as the resulting combination of province and level are not desirable, and secondly the "`pro'" "_" "`map'" goes wrong. And following your advice, I try to substitute "`pro'_`map'" for "`pro'" "_" "`map'", and it works partly! Just works 'partly' because the nested forvalues are not perfect as I have mentioned as firstly wrong.
      If I delete shp2dta using "`pro'" "_" "`map'" …… in the loop, I get:
      Code:
      Qinghai_state
      Shaanxi_city
      Shanghai_county
      Shaanxi_state
      Shanghai_city
      Shanxi_county
      Shanghai_state
      Shanxi_city
      _county
      Shanxi_state
      _city
      _county
      and with the full loop posted in #1, I get:
      Code:
      Qinghai_state
      invalid '"_' 
      r(198);
      
      end of do-file
      
      r(198);

      Comment


      • #4
        The loops automatically bump up their own counters, so this structure

        Code:
        forvalues n = 1/4 {    
            forvalues t = 1/3 {
                local n = `n'+1        
                local t = `t'+1    
            }
        }
        should almost certainly be just

        Code:
        forvalues n = 1/4 {    
             forvalues t = 1/3 {    
             }
        }

        Comment


        • #5
          Hi Nick, the code now works "wholly"! Before opening this thread, I tested my codes nearly three hours tossing and turning, and now the superfluous " "_" " and local n = `n'+1 turn out that I just carry coals to Newcastle. Thank you as always! Now the codes is:
          Code:
          local province "Qinghai Shaanxi Shanxi Sichuan Xinjiang Yunnan Zhejiang"
          local maptype "province city county"
          forvalues n = 1/7 {
           forvalues t = 1/3 {
             local pro: word `n' of `province'
             local map: word `t' of `maptype'
             display "`pro'_`map'"
             /*shp2dta using "`pro'_`map'", data("`pro'`map'"_label) coor("`pro'`map'"_map)*/ /*ignore it for not having necessary .shp files*/
           }
          }
          Last edited by Chen Samulsion; 09 Nov 2018, 13:03.

          Comment


          • #6
            Dear Nick Cox, I think I should take this chance to dig and learn more. As you have pointed to me, "`pro'_`map'" is right, and "`pro'" "_" "`map'" is wrong.
            Code:
            "`pro'" "_" "`map'"
            Code:
            "`pro'_`map'"
            What's the difference between these two expressions, as we can see in the third code below, they both give seemingly same results:
            Code:
            local province "Qinghai Shaanxi Shanxi Sichuan Xinjiang Yunnan Zhejiang"
            local maptype "province city county"
            forvalues n = 1/7 {
             forvalues t = 1/3 {
               local pro: word `n' of `province'
               local map: word `t' of `maptype'
               display as text "`pro'" "_" "`map'"
               display as result "`pro'_`map'"
             }
            }
            And I would add that, using the codes in #5, the resulting .dta files' name is a bit confusing 'cause there is a blank space between "`pro'`map'" string and "_map" string, just like this:
            Code:
            Qinghaicounty _map.dta

            Comment


            • #7
              The difference lies in what display does. display will strip quotation marks and ignore spaces outside literal strings as well as replace macro references by their contents. That's part of its job. But other commands don't always work on the same way on the same input.

              Code:
              . di "frog" "_" "toad"
              frog_toad
              
              . di "frog"  "_"  "toad"
              frog_toad

              Comment


              • #8
                Thank you Nick! I get it.
                Code:
                display as text "I'm" "      " " a- bunny" as error "And more important, congratulations on your posts >=16000!"
                I'm       a- bunnyAnd more important, congratulations on your posts >=16000!
                Last edited by Chen Samulsion; 10 Nov 2018, 04:46.

                Comment

                Working...
                X