Announcement

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

  • help with loops

    I'm trying to understand loops. I have 20 sting variables: sic1 to sic20. For each, I want to check if the variable is equal to, say, '2833' or '2899'. If it is, I want to set a dummy equal to 1. I know that this is probably pretty basic stuff, but i don't get loops and would like to, so i hope someone can help. thanks in advance!

  • #2
    You don't give a data example, but here is what I believe you want

    Code:
    foreach v of var sic* {
    
    qui g dummy_`v' = 1 ///
        if inlist(`v', "2833","2839")
    }
    Without seeing your real data, using dataex, I can't further comment, but maybe there's a better way of doing what you really want that someone could suggest upon seeing your real data.

    Comment


    • #3
      Jared, super, that works perfectly, thank you so much!

      Comment


      • #4
        Be sure to replace all the missing values with ""

        I'm not at my computer, but you could theoretically use the cond function to do it all in one shot. You're most welcom.

        If nobody's said it yet, welcome to Statalist.

        Comment


        • #5
          Thanks Jared! Super helpful; I'm already starting to play around with it successfully

          Comment


          • #6
            or

            Code:
            qui g dummy_`v' = (inlist(`v', "2833","2839"))

            Comment


            • #7
              Nicola Tommasi Stata just knows to generate a 1?

              Comment


              • #8
                Here's an simple example

                Code:
                . tab NPG2
                
                       NPG2 |      Freq.     Percent        Cum.
                ------------+-----------------------------------
                          A |        880       84.94       84.94
                          B |        139       13.42       98.36
                          C |         17        1.64      100.00
                ------------+-----------------------------------
                      Total |      1,036      100.00
                
                . gen flag = (inlist(NPG2, "A"))
                
                . tab NPG2 flag
                
                           |         flag
                      NPG2 |         0          1 |     Total
                -----------+----------------------+----------
                         A |         0        880 |       880
                         B |       139          0 |       139
                         C |        17          0 |        17
                -----------+----------------------+----------
                     Total |       156        880 |     1,036

                Comment


                • #9
                  Stata just knows to generate a 1?
                  inlist() is a function which returns a 1 or a 0, as the output of help inlist() tells us. Assigning its value directly is preferable to the code in post #2, in that it generates a standard 0/1 indicator variable rather than a missing/1 variable.
                  Last edited by William Lisowski; 17 Mar 2022, 11:06.

                  Comment


                  • #10
                    Code:
                     
                      qui g dummy_`v' = (inlist(`v', "2833","2839") if `v'!=""

                    Comment


                    • #11
                      Thanks for the additions, William and Nicola!

                      A follow-up question if I may. My data looks approximately like this:

                      ID1 ID2 ID3 ... . ID20 Flag1 Flag2 Flag3 ... Flag 20 ID_final Dup
                      1. F1 F26 F7 F56 1 0 0 0 F1 0
                      2. F8 F64 F17 F24 0 1 0 0 F64 0
                      3. F10 F26 F79 F78 1 0 0 1 F10 1
                      4. F10 F26 F79 F78 1 0 0 1 F78 2

                      The observations in my sample can have up to 20 IDs (ID1-ID20). When one of these matches with another sample, I indicate that with a Flag. On row 1 above, Flag1 implies that F1 is the ID that I am interested in. I then create an ID_Final and set this equal to F1.

                      The problem is that I can have multiple relevant IDs. In row 3, for example, I match both F10 and F78. I am trying to solve this by duplicating row 3 to create row 4. My thinking is that I would set ID_final = F10 if dup=1 and set ID_final = F78 if dup =2.

                      I have 20 IDs, 20 flags, and there can be up to 17 ID hits on the same observation. I am sure there is an easy way to do this with loops, but I am not experienced enough to think of how to do it now, and I hope, therefore, that you might have some suggestions on how to proceed.

                      Thanks in advance,

                      Killian

                      Comment


                      • #12
                        Please use the command dataex to show us your real data.


                        And also, I think a slightly better description would be more helpful. in my work, when I think of an ID, I'm thinking of a number that is meant to denote a specific unit in a panel dataset, 1=Chicago, 2=Beijing.... and so on.

                        I'm also unclear on what the flags mean or why they'd be useful, so... I think a data example (that's in the code delimiters) and a better description of the problem would help a lot here.

                        Comment


                        • #13
                          Jared,

                          sorry for the confusion; I wasn't aware of dataex.

                          Each row is an alliance and it has a unique deal number. The cusip codes are unique firms 'IDs'. I have up to 20 firms per alliance. The flags that i mentioned are called base1-20 in my data. These are dummy variables to indicate that that firm was present in another of my data sets. Base1 indicates that the firm in the cusips1 column is interesting.

                          'Sum' = the number of firms per alliance that I am interested in. When sum=1 there is only one firm in that alliance that i am interested in. I then copy the corresponding cusip code over to 'match'. When there are 2 firms in the same alliance that are interesting, I duplicate the alliance. Suppose that there are 2 firms in the same alliance: I then want to set the first one equal to 'match' and the second one equal to match in the duplicate case.

                          The problem is that there are 20 cusips (IDs), 20 indicators that the firm is interesting (base1-20), and dozen of combinations. For example, in one alliance, cusip1 and 2 could be interesting but in the next cusip 4 and 19 could be of interest. In one alliance there might be 2 duplicates and in another 13. So i'm a little bit lost on how to code this.

                          Below a snippet from my data using dataex; I only included the first 10 (cusip and base variables) to make it readable. Any thoughts would be much appreciated!
                          Last edited by killian mccarthy; 20 Mar 2022, 08:39.

                          Comment


                          • #14
                            input str10 DealNumber byte dup str6(cusip1 cusip2 cusip3 cusip4 cusip5 cusip6 cusip7 cusip8 cusip9 cusip10) float(base1 base2 base3 base4 base5 base6 base7 base8 base9 base10 sum) str6 match
                            "389491045" 0 "305204" "369604" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "369604"
                            "974280045" 0 "606769" "459200" "606776" "" "" "" "" "" "" "" . 1 1 . . . . . . . 2 ""
                            "846546045" 0 "866810" "871503" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "866810"
                            "1028699045" 0 "69825J" "620076" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "620076"
                            "229324045" 0 "459200" "46070J" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "459200"
                            "737604045" 0 "478160" "75255Y" "74846X" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "478160"
                            "409168045" 0 "266354" "478160" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1855122045" 0 "00823Y" "82328J" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "82328J"
                            "493453045" 0 "138084" "717081" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1077949045" 0 "17275R" "88660Q" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "17275R"
                            "1469001045" 0 "05786H" "00516Q" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "00516Q"
                            "320513045" 0 "827056" "52769" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "827056"
                            "931093045" 0 "796050" "879369" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "453797045" 0 "05538E" "982351" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "05538E"
                            "2335574045" 0 "97023" "826197" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "826197"
                            "938353045" 0 "68389X" "826197" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "826197"
                            "1054792045" 0 "26903P" "459200" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "459200"
                            "235911045" 0 "628546" "367408" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "628546"
                            "368684045" 0 "892314" "874058" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "425983045" 0 "866810" "23905" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "866810"
                            "308726045" 0 "87402W" "D1668R" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "D1668R"
                            "961436045" 0 "23246Z" "471896" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "471896"
                            "1236873045" 0 "826197" "96766K" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "826197"
                            "1028667045" 0 "806879" "92219H" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "806879"
                            "995041045" 0 "91813L" "00256R" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "00256R"
                            "1858328045" 0 "03615J" "233806" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "03615J"
                            "1188128045" 0 "422806" "422806" "1765" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "541642045" 0 "55646Z" "594918" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "55646Z"
                            "481059045" 0 "370121" "80312F" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "370121"
                            "218773045" 0 "369604" "433578" "891493" "" "" "" "" "" "" "" 1 1 1 . . . . . . . 3 ""
                            "1441533045" 0 "53220K" "53220K" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "375016045" 0 "65473A" "98946Q" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "98946Q"
                            "983538045" 0 "656568" "44935J" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1291011045" 0 "796050" "52769" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "796050"
                            "2278070045" 0 "819882" "16835F" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1930951045" 0 "89302A" "904JVP" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "89302A"
                            "862611045" 0 "72730" "808905" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "808905"
                            "962421045" 0 "46631B" "459200" "25253N" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "459200"
                            "682617045" 0 "76132M" "253849" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "253849"
                            "407075045" 0 "891493" "459200" "826197" "" "" "" "" "" "" "" 1 1 1 . . . . . . . 3 ""
                            "214066045" 0 "45765P" "171629" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1028409045" 0 "50156V" "52728R" "934488" "" "" "" "" "" "" "" 1 1 1 . . . . . . . 3 ""
                            "2286926045" 0 "44334L" "500472" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "500472"
                            "935849045" 0 "464330" "2824" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "464330"
                            "343559045" 0 "989524" "459864" "01748B" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "989524"
                            "1532578045" 0 "452521" "297425" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "423050045" 0 "1957" "458140" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "458140"
                            "363604045" 0 "370121" "620076" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1925711045" 0 "742718" "369604" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1457198045" 0 "92592N" "17275R" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "17275R"
                            "532532045" 0 "444903" "723686" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "444903"
                            "326267045" 0 "670006" "253849" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "253849"
                            "375434045" 0 "774347" "579468" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "774347"
                            "535803045" 0 "46005H" "45763R" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "229254045" 0 "392896" "74727I" "392896" "" "" "" "" "" "" "" 1 . 1 . . . . . . . 2 ""
                            "538429045" 0 "00744F" "D1668R" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1996418045" 0 "641069" "82466Q" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "400642045" 0 "233829" "80688T" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "453364045" 0 "760911" "803038" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "803038"
                            "1524568045" 0 "428236" "05577E" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "428236"
                            "1467294045" 0 "228769" "876033" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "228769"
                            "1892889045" 0 "37733W" "87611R" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "981981045" 0 "64051E" "459200" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "392527045" 0 "80527E" "12637R" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1511199045" 0 "81602J" "803038" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "425760045" 0 "606776" "78081A" "94957W" "94957W" "" "" "" "" "" "" 1 . . . . . . . . . 1 "606776"
                            "684088045" 0 "2824" "37244T" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "37244T"
                            "230420045" 0 "638882" "654624" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "654624"
                            "989704045" 0 "79581F" "428236" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "428236"
                            "393567045" 0 "63632R" "05534B" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "05534B"
                            "178485045" 0 "882508" "29585K" "74150Q" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "882508"
                            "661135045" 0 "89614D" "428236" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "569695045" 0 "579064" "00503U" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "00503U"
                            "1511666045" 0 "866810" "45614H" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "866810"
                            "555759045" 0 "460593" "55480A" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "460593"
                            "1232141045" 0 "284155" "904JVP" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "284155"
                            "233736045" 0 "500472" "23404P" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "500472"
                            "230520045" 0 "699421" "81765P" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "699421"
                            "1751450045" 0 "50390L" "03386C" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "03386C"
                            "1397016045" 0 "501556" "19193H" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "501556"
                            "860179045" 0 "628530" "88579Y" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1685560045" 0 "58501R" "88615N" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "58501R"
                            "2203134045" 0 "359590" "84802N" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "359590"
                            "1662675045" 0 "25433R" "866810" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "866810"
                            "1908728045" 0 "594918" "49178Q" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "49178Q"
                            "1076753045" 0 "589339" "H8477N" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "589339"
                            "1924284045" 0 "724479" "82995V" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "724479"
                            "1504894045" 0 "817523" "67012A" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "817523"
                            "457497045" 0 "55646Z" "17275R" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "310411045" 0 "23282E" "66987V" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "66987V"
                            "522659045" 0 "806879" "17159Q" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "806879"
                            "327664045" 0 "37833" "501902" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "501902"
                            "411336045" 0 "949044" "78378A" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "949044"
                            "949731045" 0 "83088M" "45656X" "64143R" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "83088M"
                            "946948045" 0 "44102T" "369604" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "369604"
                            "399172045" 0 "371532" "147618" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "147618"
                            "467490045" 0 "695934" "620076" "" "" "" "" "" "" "" "" 1 1 . . . . . . . . 2 ""
                            "1794991045" 0 "82371R" "882508" "" "" "" "" "" "" "" "" . 1 . . . . . . . . 1 "882508"
                            "1751770045" 0 "369604" "577081" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "369604"
                            "495381045" 0 "46367M" "12016J" "" "" "" "" "" "" "" "" 1 . . . . . . . . . 1 "46367M"
                            end
                            [/CODE]

                            Comment


                            • #15
                              #11 I can't follow what you want to do and why, but your existing data structure is going to be hard to work with in Stata.

                              Something like

                              Code:
                              reshape long cusip base, i(Deal)
                              drop if missing(cusip)
                              gets you quickly to something different.

                              Comment

                              Working...
                              X