Announcement

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

  • Loop through string to manipulate variable

    Hi,

    I am having a hard time to loop over a string (not a variable) in order to manipulate a variable defined by the string. Please help.

    Here is what I have:
    Code:
    sysuse auto, clear
    
    local factors displacement gear_ratio foreign
    foreach var in `factors' { 
        gen r_`var'=`var'/100
    }
    drop displacement gear_ratio foreign
    
    foreach var in `factors' { 
        replace r_`var'=`var'/100
    }

  • #2
    You want to loop over the contents of a local macro, hence you have to specify -foreach- differently (see -help foreach-):
    Code:
    local factors displacement gear_ratio foreign
    foreach var of local factors {
        gen r_`var'=`var'/100
        drop `var'
    }
    Are you sure to divide "foreign" (a dichotomous variable with values 0 and 1) by 100?

    I don't know why you want to drop "displacement", "gear_ratio", and "foreign". You could also consider to replace the values of these variables by dividing their values by 100 and by modifying the variable labels accordingly:
    Code:
    sysuse auto, clear
    
    local factors displacement gear_ratio foreign
    foreach var of local factors {
        replace `var'=`var'/100
        local varlab : variable label `var'
        label variable `var' "`varlab'/100"
    }
    Last edited by Dirk Enzmann; 12 Apr 2022, 02:02.

    Comment


    • #3
      Dirk,

      I am dropping these variables (displacement gear_ratio foreign) on purpose because I do not want them to be variables since the strings I want to loop over are NOT available as variables. And the division by 100 is also just an example, I could have added +10 or -5, it does not matter... I do not want to rename or relabel anything, I would like to loop over variables defined by "r_"string""

      Comment


      • #4
        OK. BTW: You are not looping over strings but over the content of a local macro that is a string containing variable names.

        Comment


        • #5
          To give you a bit of context, I am pooling stock tickers from Yahoo from a string list: "AAPL MSFT GE GM ..." 505 of them. And the subroutine I use creates the following stock returns: r_AAPL r_MSFT r_GE r_GM . I would like to loop through the return variable r_* using the ticker string list. Does it make more sense? Is there anyone who can help? Thanks

          Comment


          • #6
            And yes, I know that I am not looping the way I would like. This is what I am looking for... Do you have the answer? Thank you in advance!

            Comment


            • #7
              #3 seems to contradicts itself or #1. You want to loop over variables ... which are not variables. You gave an example ... but it's not really what you want. I can't follow.

              But note that none of these constructs

              Code:
              #1 
              foreach x in frog toad newt { 
              
              }
              or

              Code:
              #2 
              local whatever frog toad newt 
              
              foreach x of local whatever { 
              
              }
              or

              Code:
              #3 
              local whatever frog toad newt 
              
              foreach x in `whatever'
               
              }
              depends on the arguments being variable names.

              Comment


              • #8
                Here is what I have:
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input double(date R_ADM R_APH R_BIIB R_BLK R_BMY)
                18074   -.01074866822039866    .006944208390613271   .0016002799919871968    .014717776337770383  .0033508709161655936
                18077   .020981495599176725   -.013479552394356498    -.05492310764929098   -.010850029528934213   -.01860693697338711
                18078   -.01761450089761052    .005401908499503838   -.045857945900253605  -.0030125602084469794   -.01263978772242591
                18079   .029137249101689022    .022440187096177677     .03588035278492341   -.014650584933468047   .012000175599663265
                18080    -.0366604597159329   -.022565683277435356    -.03784477228992933    -.03008357305532816  -.023221685936028025
                18084    .00791214025413467    .003795178444290892    .004888911111111005   -.008470321389028368 -.0035403394641650987
                18085    .01794405520158815   -.014177797849998286    -.01614327695393014   -.024423797292705718  -.001015152647271922
                18086  -.025339393444845926   -.010866106503894857    .008316452761678805   -.004809602073038377                     0
                18087   .018462150194800353    .019063128092111414    .012706174482697952    .030361174521240875  -.009146589133447014
                18088  -.013688345786898592    -.00317078689387551    -.01496808276469297  -.0007816689984095904  -.006666444279585626
                18091   .015754260626604788     .01781176448458699    .031508379888268045     .03574869801275127    .00567920959793744
                18092   .006646554911194924    .006249862944626594   -.004766052859618619     .00354421949452588 -.0005138964062917362
                18093   .024945326316130334     .05434814937154209    .015890270263175132    .042615102970349206   .020031011195385195
                18094    .01896884486322148   -.010604010809924014    .009213627992870288    -.00527641633572884   .013595419816808452
                18095  .0056197311047667675   -.017862461764167614   -.021868323646213272   .0037408035205770407  -.006458032159008198
                18098   .012574560657240053    .003637439702647738    .018450141089646266     .02797753123552149  -.006000237746565662
                18099    .00931335928298882    .009362552031209948     .01790281406235339   -.003679420066503236   .011569896899931598
                18100  .0071772530709339755    .015260282380856527    -.01884415908057613    .007439865815387803   .008950629774906238
                18101   .042755178678718364     .00795756934224734    .036918458452444236     .06678876648966381   .028092092484419684
                18102  -.011063855620646157    .014327278001714738    .008026342868903136   -.028903508603970174    .02588713368119966
                18105   .051003423234860845    -.00922415873634208     .00469577378521846   -.011343438174240772 -.0009345096200548131
                18106   -.02567278604911749   -.006983169025614133   -.003860983618390268   .0008420689752635102  -.009354623068275836
                18107  -.012532555731327388   -.021681053288474694    -.01835987352101176  -.0048381342965339025   .019358277366076315
                18108  -.016596485656366733   .0017964028325299086   -.015793807477011867     .01120295833649565 -.0013900997597260378
                18109  -.003308431871291484   -.002989471539856181    .004011782009886389   -.004285244162652524   .008349163553858174
                18112   .008964090850640538    .030584754429500594    .017034742734694852    .026660805416665267   .004139278616583475
                18113  -.051990986452741805   -.009019322074161562    .005169561514277055    .015233655348496855   .004123194882529546
                18114 -.0017357547205917312   -.021139399687851723   -.020366220523221124    .010221707080787512  -.007755556538539432
                18115  -.003476577444129325   -.007498336718141596   -.020369530037159356   -.013656967137631315 -.0009198225982200992
                18116  .0013954727651583898   .0072527604142977875    .013504758263461536     .02309361789026343   .007823441711441781
                18119 -.0006966913032725452  -.0014999849048128455    .013747906382147068    -.01822569253224582 -.0009132436646770437
                18120 -.0066249292483284705   -.015925608440966355  -.0025036302941789934 -.00005020659984478204    .00411349255987048
                18121  -.017901073337260653     .03511427626960795    .024890168900017384    .011672027778113936  -.003641335159004999
                18122   .007505639280500631    .003540058959322634  -.0016326938775510502   -.012382648476454095 -.0013709096365258633
                18123  -.011351638703284483    -.01704901912454171  -.0028617948839655405    -.00412923192064063 -.0022867985407182267
                18126   -.02942255917863623    -.03080150861838571   -.010865088373617993    -.04454671959463963  -.006418944132558391
                18127   .020066834721074178  -.0021596989820819035    .002072497409326468    .038156198002359476   .004614540566143743
                18128   .008379050954803624  -.0012366678148125754    .014064137086417183   .0023958795397640564    .01929256548468672
                18129   .010476660534182614    .020433427056794518    .011013685723305855    .048769164620614314   .002703856337856969
                18130   .025027190227007612     .01183250672179792    .008069416986080213    .006158495119945817   .014382253028171421
                18133  -.009766632879511072    .007495955857442782   -.008805323017704203   -.013012450977117895  .0004431343303878954
                18134   -.02289505958214566    .004464583597127758   .0036341611878489832   -.011914012168072086    .01638613810835432
                18135   .005767623460334326    .024888842278039558    .004224482080557035   -.015615519413987644 -.0017429884750650437
                18136   .011111178403649058    .007227492490341048   .0026041868030524107    .005270940273835345  -.005674586649618507
                18137    .01453394346555803    .013777423707145742   -.002397582465486159    .006491889849336241  -.028972565514878257
                18140   .007337421201397465   -.010192701944437998   .0054075906268775675   -.005976859436141346   .000451597039588858
                18141  -.027748679806144372   -.019450958787761152    -.01912348567483102   -.027308526925156396  -.019882345213258823
                18142  -.016767989659100535  -.0002909622740374651  -.0010154954908409009    -.00896410465643327  .0018440303968636496
                18143   .016690761165547574    .021301031413754057     .01463714199302979     .02474309777138206  .0018411280934251817
                18144    .03176340815599632    .011428446773799283     .02203963133640552   -.008572548418209203   .010564664045759509
                18148  -.005880577818942245    .051977613563813696   .0033327192053604904    .020004972779497532  .0013641248626088398
                18149 -.0013914222023525546    .013694910835177598   -.002735423993747599    .013292228857748833    .01134817811576383
                18150   .010801165417855252     .01960254550636842   -.013910638442189649  -.0016335414926782477 -.0008980785683049275
                18151 .00034446279987965234  -.0018187749354880989    .010133081258371313  .00044679829013660743   .001797154310176019
                18154  -.012749707402894161 -.00039067736516895657 -.00019665617623909615  -.0026768906945729976   .004035812598609967
                18155   .017801015482654003    .007814532580586064    .011410544504798503  -.0027823494225844847  -.006252802085440434
                18156   .008916281153229288    .024296025744145355   .0027231861505543353     .03134467031540944   .009438149812991181
                18157  -.023453417997070014   .0030279180725520938   -.021338467921211746    -.02188842964145732  -.005342909130365392
                18158   .013574668486354025   -.009559868905149482   -.008919742142324272     .02351447605464374   .008057379549581523
                18161  -.009272071121169678   -.003048139242209702    .017799979999999938   -.013852241869685145  .0022205237992498628
                18162  .0017334505353543417  -.0030568968108217742   -.007074081490942796      .0189900643275982  -.013292203251036305
                18163  -.013148848393275347   -.016100708318874002   -.005937047416129992    .013592854802045862 -.0013470374706248123
                18164   -.01928503728485864   -.018181776513491288                      0   -.022650787603373912   .008543316860913697
                18165  -.023596571307917813   -.014020476050313202  -.0023889906430419883   -.012121614987049112 -.0004458321434150925
                18168    .01391438348748264    .006707407077288119    .009179784290964221     .04991425524392645    .01204249642235311
                18169   .018418068368910134  -.0018654198082304063    .016610638718607795   .0009352013006960203   .008374017322341443
                18170    .03617034892649973    .006141415112376425    -.01731180704143165    .012609431703625607 -.0022154833703936783
                18171  -.012320261774042107    -.04379001334378185   -.023357086302454477     -.0369890376831612  -.018649790260005905
                18172  -.014899251848423223    -.01554246554812233    -.00912040940413461   -.008956158378105372   .007239610031610555
                18175   .011606919195860765     .01494171471328487   -.007158887444444373     .02981648244864399 -.0026953067139541265
                18176   .024687182581120792     .02777822370687333    .009888730739828322 .000047530013075558486   .011711408710645133
                18177  .0010178223001731102     .01486469761597764   .0030599347205222394   .0018299901475269387 -.0062329585354009565
                18178   .007457943256531152    .008788372838108627    -.01159243488275108   .0016857919666597443  .0008955253896294479
                18179  .0006727004502875555    .006071818307810339    .008436214338938708    .012578782028635382  .0058191455728315855
                18182   .003699111659010379      .0196798643286904   -.006529259601275594    .005448844021206822    .01023650238679652
                18183  .0033500133374144703    -.01286661906129627    .002259190845331549   -.018096112513591334  -.004405523994787663
                18184 -.0013356700198151605     .07638161954753397     .02540987756987456     .05739335603842832    .01504418320308476
                18185  -.008358373306994404   -.006781562859032753   -.004996003097601842  .00044220838868080925   .011769612694330522
                18186   .011126143691932766   -.004876678647606658    -.01184976879193067   -.002697344937769963  -.014217622248227313
                18189   .023340671575464045    .013722005292013506   .0038617478889888535    .021636589942368722   .003933535559852874
                18190 -.0026062527139534275   -.007493485189356219   -.026928488093308056    .026775679984951006   .003047677103607649
                18191   .024175187932536257    -.02581579504661391    -.03391595851194429   -.011792144858954856  -.015191235060021402
                18192  -.008612612510722206    .038750135657734885     .01723023906956711 -.00004233421287402894  -.010136847675971339
                18193  -.013513855563321298   -.006498213421324062    -.07241158162185046   -.011121093601111909  -.009795013190547097
                18196  -.009132232797490758    .004118272379660448    .007304268265138747   -.013883985737474402  -.014388686544797968
                18197  -.009216535868505571    .004101275339748911    .009517289609850765    -.02671208531314906   .009124583904653427
                18198  -.017275711094794755    -.03531945863049846   -.022895623409553867  -.0012614438306216558  .0018084021207542822
                18199    .04530101412151155    .031631241567490864    -.01033762945870953    .016424314498798998  .0018047978449007296
                18200  -.025873217507401236   -.031385224095953346    -.02205201847483662    -.03893267623840091   -.01801844956684384
                18203   .013280256512421706     .02417720944128554    .010918561335899302      .0194932122450715 -.0013756068676010758
                18204    .04554372224018777    .006570851192622129    .015496595444940198   .0014496265754762039   .004133750723726237
                18205   .009401376305969045   .0009669111588375134   -.010404647398843947  -.0030766507039592383   .014638743485801298
                18206   .006830293399985976    .015700725042304686     .03714953357826012    .028409505103517363   .014427678215441997
                18207 -.0012335282572483175  -.0028541842834691744      .0308627851061678    .003971635169087939   .006221875119454934
                18210  .0021613725951759033     .02313419605520184   -.007648558026597807    .035119494579353816   .018109439575733743
                18211   .004620804603532536    .005594648947193193    .002202114071790273   -.014522364506272312   .012581460827247204
                18212  -.013799077651910019  .00046325195097574046     .02021538212328644    .005084136255589122  -.003856269826595593
                18213   .002798103997289525   -.018999050006613394   .0015076459185872082   -.008616647274139244  -.005590931727583626
                18214   .008062063696766542  -.0016533723774281306    .004731204301075209    .008000080974496893    .00259511355356401
                18217  -.017840380227145514     .02176457806231724   -.004066823543090292    .025096232719907574   .048317360411435084
                end
                format %td date
                There are 505 of them. I have a list as a string, but without r_. That list would be here:
                global tickers "ADM APH BIIB BLK BMY"

                I would like to manipulate the variables r_*. For instance:
                replace r_"string" = r_"string"*252

                Does that make sense?

                Comment


                • #9
                  Try
                  Code:
                  local names "ADM APH BIIB BLK BMY"
                  foreach suffix in `names' {
                     replace R_`suffix' = R_`suffix'*252
                  }
                  Last edited by Dirk Enzmann; 12 Apr 2022, 02:45.

                  Comment

                  Working...
                  X