Announcement

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

  • Help with Strpos

    Hi there,

    In the code below my goal is to convert the local below into a neat list by converting "R140/R143" to "R140" "R141" "R142" "R143" and so on. Below is my code, it seems to be running correctly but it crashes in the last line, when I ask it to display local GC so I can make sure the local has been transformed as I expected it to transform. How do I have stata display a local back to me?

    cls

    clear

    local GC `" "R070" "R140/R143" "R09" "R093" "R12" "R120" "R150 / R152" "R190 / R196" "R200 / R203" "R208 / R209" "R21" "R210" "R22" "R220 / R224" "B078 / B079" "K00" "K000 / K009" "K01" "K010 / K011" "K02" "K020 / K029" "K03" "K030 / K039" "K04" "K040 / K049" "K05" "K050 / K056" "K06" "K060 / K062" "K068 / K069" "K07" "K070 / K076" "K078/ K079" "K08" "K080 / K085" "K088 / K089" "K09" "K090/K092" "K098 / K099" "K10" "K100 / K103" "K108/K109" "K11" "K110 / K119" "'

    set trace off

    foreach x of local GC{
    if strpos("`x'","/")>0 & strpos("`x'","0")!=2 {

    local CODE =substr("`x'",1,1)
    *di "`x'"
    *di "`CODE'"

    foreach a in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z {
    local x=subinstr("`x'","`a'","",.)
    }
    di "`CODE'"
    *di "`x'"

    local x=subinstr("`x'"," ","",.)
    forvalues z=`x' {
    di "`CODE'`z'"

    }
    }
    }

    foreach x of local GC{
    if strpos("`x'","/")>0 & strpos("`x'","0")==2 {

    local CODE =substr("`x'",1,2)
    *di "`x'"
    *di "`CODE'"

    foreach a in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z {
    local x=subinstr("`x'","`a'","",.)
    }
    di "`CODE'"
    *di "`x'"

    local x=subinstr("`x'"," ","",.)
    forvalues z=`x' {
    di "`CODE'`z'"

    }
    }

    else {
    di "`x'"

    }
    }

    }
    di "`GC'"

  • #2
    Can you expand them as a numlist?

    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿ#delimitÿ;
    delimiterÿnowÿ;
    .ÿlocalÿGCÿ`"ÿ"R070"ÿ"R140/R143"ÿ"R09"ÿ"R093"ÿ"R12"ÿ"R120"ÿ"R150ÿ/ÿR152"ÿ
    >ÿÿÿÿÿÿÿÿÿ"R190ÿ/ÿR196"ÿ"R200ÿ/ÿR203"ÿ"R208ÿ/ÿR209"ÿ"R21"ÿ"R210"ÿ"R22"ÿ"R220ÿ/ÿR224"ÿ
    >ÿÿÿÿÿÿÿÿÿ"B078ÿ/ÿB079"ÿ"K00"ÿ"K000ÿ/ÿK009"ÿ"K01"ÿ"K010ÿ/ÿK011"ÿ"K02"ÿ"K020ÿ/ÿK029"ÿ
    >ÿÿÿÿÿÿÿÿÿ"K03"ÿ"K030ÿ/ÿK039"ÿ"K04"ÿ"K040ÿ/ÿK049"ÿ"K05"ÿ"K050ÿ/ÿK056"ÿ"K06"ÿ
    >ÿÿÿÿÿÿÿÿÿ"K060ÿ/ÿK062"ÿ"K068ÿ/ÿK069"ÿ"K07"ÿ"K070ÿ/ÿK076"ÿ"K078/ÿK079"ÿ"K08"ÿ
    >ÿÿÿÿÿÿÿÿÿ"K080ÿ/ÿK085"ÿ"K088ÿ/ÿK089"ÿ"K09"ÿ"K090/K092"ÿ"K098ÿ/ÿK099"ÿ"K10"ÿ
    >ÿÿÿÿÿÿÿÿÿ"K100ÿ/ÿK103"ÿ"K108/K109"ÿ"K11"ÿ"K110ÿ/ÿK119"ÿ"';

    .ÿ#delimitÿcr
    delimiterÿnowÿcr
    .ÿ
    .ÿ//ÿlocalÿresult
    .ÿforeachÿelementÿofÿlocalÿGCÿ{
    ÿÿ2.ÿ
    .ÿÿÿÿÿÿÿÿÿifÿ!strpos("`element'",ÿ"/")ÿ{
    ÿÿ3.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ//ÿNoÿop
    .ÿÿÿÿÿÿÿÿÿ}
    ÿÿ4.ÿÿÿÿÿÿÿÿÿelseÿ{
    ÿÿ5.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿelementÿ:ÿsubinstrÿlocalÿelementÿ"ÿ"ÿ"",ÿall
    ÿÿ6.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿcharacterÿ=ÿsubstr("`element'",ÿ1,ÿ1)
    ÿÿ7.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿelementÿ:ÿsubinstrÿlocalÿelementÿ"`character'"ÿ"",ÿall
    ÿÿ8.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿexpanded
    ÿÿ9.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿforeachÿnumeralÿofÿnumlistÿ`element'ÿ{
    ÿ10.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿexpandedÿ`expanded'ÿ`numeral'
    ÿ11.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ}
    ÿ12.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿexpandedÿ:ÿsubinstrÿlocalÿexpandedÿ"ÿ"ÿ"ÿ`character'",ÿall
    ÿ13.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlocalÿelementÿ`character'`expanded'
    ÿ14.ÿÿÿÿÿÿÿÿÿ}
    ÿ15.ÿ
    .ÿÿÿÿÿÿÿÿÿlocalÿresultÿ`result'ÿ`element'
    ÿ16.ÿ}

    .ÿ
    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ80

    .ÿdisplayÿinÿsmclÿasÿresultÿ"`result'"
    R070ÿR140ÿR141ÿR142ÿR143ÿR09ÿR093ÿR12ÿR120ÿR150ÿR151ÿR152ÿR190ÿR191ÿR192ÿR193ÿR1
    >ÿ94ÿR195ÿR196ÿR200ÿR201ÿR202ÿR203ÿR208ÿR209ÿR21ÿR210ÿR22ÿR220ÿR221ÿR222ÿR223ÿR2
    >ÿ24ÿB78ÿB79ÿK00ÿK0ÿK1ÿK2ÿK3ÿK4ÿK5ÿK6ÿK7ÿK8ÿK9ÿK01ÿK10ÿK11ÿK02ÿK20ÿK21ÿK22ÿK23ÿK
    >ÿ24ÿK25ÿK26ÿK27ÿK28ÿK29ÿK03ÿK30ÿK31ÿK32ÿK33ÿK34ÿK35ÿK36ÿK37ÿK38ÿK39ÿK04ÿK40ÿK41
    >ÿÿK42ÿK43ÿK44ÿK45ÿK46ÿK47ÿK48ÿK49ÿK05ÿK50ÿK51ÿK52ÿK53ÿK54ÿK55ÿK56ÿK06ÿK60ÿK61ÿK
    >ÿ62ÿK68ÿK69ÿK07ÿK70ÿK71ÿK72ÿK73ÿK74ÿK75ÿK76ÿK78ÿK79ÿK08ÿK80ÿK81ÿK82ÿK83ÿK84ÿK85
    >ÿÿK88ÿK89ÿK09ÿK90ÿK91ÿK92ÿK98ÿK99ÿK10ÿK100ÿK101ÿK102ÿK103ÿK108ÿK109ÿK11ÿK110ÿK1
    >ÿ11ÿK112ÿK113ÿK114ÿK115ÿK116ÿK117ÿK118ÿK119

    .ÿsetÿlinesizeÿ`line_size'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    Code:
    foreach element of local GC {
    
        if !strpos("`element'", "/") {
            // No op
        }
        else {
            local element : subinstr local element " " "", all
            local character = substr("`element'", 1, 1)
            local element : subinstr local element "`character'" "", all
            local expanded
            foreach numeral of numlist `element' {
                local expanded `expanded' `numeral'
            }
            local expanded : subinstr local expanded " " " `character'", all
            local element `character'`expanded'
        }
    
        local result `result' `element'
    }

    Comment


    • #3
      The problem here is with elements that have 0 as the second character, so K098 / K099 got expanded as K98 K99

      Comment


      • #4
        You're right; I hadn't noticed that. Try this instead:
        Code:
        foreach element of local GC {
        
            if !strpos("`element'", "/") {
                // No op
            }
            else {
                local element : subinstr local element " " "", all
                local character = substr("`element'", 1, 1)
                local element : subinstr local element "`character'" "", all
                local expanded
                foreach numeral of numlist `element' {
                    local numeral : display %03.0f `numeral'
                    local expanded `expanded' `numeral'
                }
                local expanded : subinstr local expanded " " " `character'", all
                local element `character'`expanded'
            }
        
            local result `result' `element'
        }

        Comment


        • #5
          Now it won't run the code at all. I made some progress,
          Click image for larger version

Name:	Screenshot 2022-05-09 16.36.10.png
Views:	1
Size:	292.1 KB
ID:	1663720

          Comment


          • #6
            So here is what I did. I broke it into three chunks a below, but now it is missing some of the elements.

            foreach var of varlist rac_icd01 rac_icd02 {
            set trace off
            forvalues i = 1/5{
            foreach x of local GC_Cat`i'{
            if strpos("`x'","/")>0 & strpos("`x'","0")!=2 {

            local CODE =substr("`x'",1,1)
            di "`x'"
            di "`CODE'"

            foreach a in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z {
            local x=subinstr("`x'","`a'","",.)
            }
            di "`CODE'"
            *di "`x'"

            local x=subinstr("`x'"," ","",.)
            forvalues z=`x' {
            di "`CODE'`z'"
            replace `var'_code= "Cat`i'" if `var'=="`CODE'`z'"

            }
            }
            }
            }
            }


            foreach var of varlist rac_icd01 rac_icd02 {
            forvalues i = 1/5{
            foreach x of local GC_Cat`i'{
            if strpos("`x'","/")>0 & strpos("`x'","0")==2 {

            local CODE =substr("`x'",1,2)
            di "`x'"
            di "`CODE'"

            foreach a in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z {
            local x=subinstr("`x'","`a'","",.)
            }
            di "`CODE'"
            *di "`x'"

            local x=subinstr("`x'"," ","",.)
            forvalues z=`x' {
            di "`CODE'`z'"
            replace `var'_code= "Cat`i'" if `var'=="`CODE'`z'"
            }
            }
            }
            }
            }

            foreach var of varlist rac_icd01 rac_icd02 {
            forvalues i = 1/5{
            foreach x of local GC_Cat`i'{
            if strpos("`x'","/") ==0 {
            di "`x'"
            replace `var'_code="Cat`i'" if `var'=="`x'"

            }
            }
            }

            }

            Comment


            • #7
              Originally posted by Harshada Karnik View Post
              Now it won't run the code at all. I made some progress,
              I don't know what you're trying to do there or even what you're trying to show in the screenshot, but my code does execute and it does what you sought to do in your original post ("my goal is to convert the local below into a neat list").

              Comment


              • #8
                Ok, i will try the code you shared again. Maybe I was missing something the first time I tried it.

                Comment

                Working...
                X