My data is as below:
I want to create a new variable PROD such that: for every (cusip yr) combination it takes the two observation i) when mn==5 ii) when mn==6 and multiples the r1000 of i) and r2000 of ii) to give PROD. So prod will be a 0/1 since r1000 and r2000 are bytes.
I am unable to pick the two obervations for multipication. It seems a little complicated.
So for example for cusip== 216485102 and year ==2001 it will take the observation where mn==5 and multiply its r1000 with the observation where mn==6 to give me PROD. Now I can set a loop for all cusip and year combination in the dataset.
One way I can think is break it into two datasets for each year, one where mn==5 and one where mn==6. Then merge both of them and thus essentially I will get the columns of the new dataset where mn==5 and mn==6 and then can simply multiple. Finally I can append all the years. But it seems too long and unncessairly complicated.
Thanks a lot!
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str8 date str9 cusip str6 ticker float(mktvalue r3000_wt r1000_wt r2000_wt) double(dt mn yr) byte(r1000 r2000) "20010531" "216485102" "CSAV" 1000.96 0 . 1.00e-06 15126 5 2001 0 1 "20010531" "538146101" "LPSN" 1903.2 0 . 2.00e-06 15126 5 2001 0 1 "20010531" "68400F109" "OPUS" 2160.5 0 . 2.00e-06 15126 5 2001 0 1 "20010531" "68617E101" "OGNC" 2555.12 0 . 3.00e-06 15126 5 2001 0 1 "20010531" "29426X102" "EDG" 2974.16 0 . 3.00e-06 15126 5 2001 0 1 "20010531" "294352109" "EPRS" 3232.8 0 . 4.00e-06 15126 5 2001 0 1 "20010531" "879202109" "TLXS" 3265.08 0 . 4.00e-06 15126 5 2001 0 1 "20010531" "64115K103" "NPLI" 3704.54 0 . 4.00e-06 15126 5 2001 0 1 "20010531" "12476Q102" "CAIS" 4340.82 0 . 5.00e-06 15126 5 2001 0 1 "20010531" "92335X100" "VRDO" 4408.36 0 . 5.00e-06 15126 5 2001 0 1 "20010531" "124269101" "BUYX" 4503.6 0 . 5.00e-06 15126 5 2001 0 1 "20010531" "232743104" "CYCO" 4856.04 0 . 5.00e-06 15126 5 2001 0 1 "20010531" "106372105" "BWAYE" 5130.06 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "90334D109" "AGA" 5146.96 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "925298101" "VATA" 5223.96 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "37243R208" "GNSL" 5238.09 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "64107E107" "NTGX" 5509.2 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "129897104" "CLIC" 5634.75 0 . 6.00e-06 15126 5 2001 0 1 "20010531" "88033A103" "TENF" 5821.34 0 . 7.00e-06 15126 5 2001 0 1 "20010531" "64114L102" "NETO" 6050.48 0 . 7.00e-06 15126 5 2001 0 1 "20010531" "64122T105" "NWKC" 6211.52 1.00e-06 . 7.00e-06 15126 5 2001 0 1 "20010531" "053566105" "AVEA" 6277.2 1.00e-06 . 7.00e-06 15126 5 2001 0 1 "20010531" "545754103" "LOUD" 6412.91 1.00e-06 . 7.00e-06 15126 5 2001 0 1 "20010531" "03828R104" "ATHY" 7004.7 1.00e-06 . 8.00e-06 15126 5 2001 0 1 "20010531" "978149102" "WOMN" 7376.22 1.00e-06 . 8.00e-06 15126 5 2001 0 1 "20010531" "92553R108" "VIAD" 7517.9 1.00e-06 . 8.00e-06 15126 5 2001 0 1 "20010531" "715329108" "PRSW" 7965.36 1.00e-06 . 9.00e-06 15126 5 2001 0 1 "20010531" "68338T106" "ONVI" 8097.3 1.00e-06 . 9.00e-06 15126 5 2001 0 1 "20010531" "37937R209" "GLBN" 8175.42 1.00e-06 . 9.00e-06 15126 5 2001 0 1 "20010531" "12500B105" "CBIS" 8273.43 1.00e-06 . 9.00e-06 15126 5 2001 0 1 "20010531" "24783N102" "DDDC" 8805.55 1.00e-06 . 1.00e-05 15126 5 2001 0 1 "20010531" "318224102" "FIRE" 9012.99 1.00e-06 . 1.00e-05 15126 5 2001 0 1 "20010531" "45073P101" "IBEM" 9046.11 1.00e-06 . 1.00e-05 15126 5 2001 0 1 "20010531" "262504103" "DSET" 9156.42 1.00e-06 . 1.00e-05 15126 5 2001 0 1 "20010531" "64120S109" "NASC" 9169 1.00e-06 . 1.00e-05 15126 5 2001 0 1 "20010531" "12877Q107" "CALD" 9389.38 1.00e-06 . .000011 15126 5 2001 0 1 "20010531" "808760102" "SCIL" 9536.32 1.00e-06 . .000011 15126 5 2001 0 1 "20010531" "87959Y103" "TGNQE" 10403.68 1.00e-06 . .000012 15126 5 2001 0 1 "20010531" "755236106" "RAZF" 10443.26 1.00e-06 . .000012 15126 5 2001 0 1 "20010531" "23437N104" "DALN" 10517.03 1.00e-06 . .000012 15126 5 2001 0 1 "20010531" "584642102" "MDLI" 10521.68 1.00e-06 . .000012 15126 5 2001 0 1 "20010531" "58446B105" "MPLX" 10726.92 1.00e-06 . .000012 15126 5 2001 0 1 "20010531" "570619106" "MKTW" 11149.95 1.00e-06 . .000013 15126 5 2001 0 1 "20010531" "29088W103" "EMRG" 11687.1 1.00e-06 . .000013 15126 5 2001 0 1 "20010531" "654113109" "NIKU" 11849.94 1.00e-06 . .000013 15126 5 2001 0 1 "20010531" "741379101" "PRVW" 11901.6 1.00e-06 . .000013 15126 5 2001 0 1 "20010531" "86149C104" "STOK" 12266.85 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "36114Q208" "FTRL" 12276.93 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "68338A107" "ONT" 12363.3 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "095180105" "BLT" 12500.4 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "922815105" "VNTR" 12608.4 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "988792107" "ZTEL" 12846 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "398081109" "GRIC" 12877.92 1.00e-06 . .000014 15126 5 2001 0 1 "20010531" "71932A101" "PHGN" 13128.83 1.00e-06 . .000015 15126 5 2001 0 1 "20010531" "918458100" "VYYO" 13271.4 1.00e-06 . .000015 15126 5 2001 0 1 "20010531" "925529208" "VYTQE" 13393.64 1.00e-06 . .000015 15126 5 2001 0 1 "20010531" "00941P106" "ANCC" 13872.87 1.00e-06 . .000016 15126 5 2001 0 1 "20010531" "284895109" "ELIX" 13917.12 1.00e-06 . .000016 15126 5 2001 0 1 "20010531" "64122G103" "NTKK" 13950.09 1.00e-06 . .000016 15126 5 2001 0 1 "20010531" "25385G106" "DIGI" 14780.5 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "36155Q109" "GCX" 14802.48 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "925653107" "VCNT" 14910.18 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "92046N102" "VCLK" 15152.94 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "53226T103" "LSPN" 15221.24 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "901314104" "TFSM" 15229.69 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "037531100" "APRN" 15266.3 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "64111K107" "NGRU" 15535.35 1.00e-06 . .000017 15126 5 2001 0 1 "20010531" "64115A105" "NTWK" 15689.16 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "530709104" "LWIRA" 15778.95 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "26861P107" "EELN" 15794.67 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "83169Q105" "SMDK" 16267.6 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "640475109" "NEOF" 16280.19 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "18681S106" "CLKS" 16282.07 1.00e-06 . .000018 15126 5 2001 0 1 "20010531" "46116T100" "IBPI" 16605.06 1.00e-06 . .000019 15126 5 2001 0 1 "20010531" "74036W102" "PRDS" 16651.8 1.00e-06 . .000019 15126 5 2001 0 1 "20010531" "64108P101" "NCNT" 16856.7 1.00e-06 . .000019 15126 5 2001 0 1 "20010531" "008447104" "ACOM" 16964.57 1.00e-06 . .000019 15126 5 2001 0 1 "20010531" "65332H203" "NEXL" 17328.72 1.00e-06 . .000019 15126 5 2001 0 1 "20010531" "53220Q105" "LFMN" 17602.2 1.00e-06 . .00002 15126 5 2001 0 1 "20010531" "83545M109" "SNCI" 17970 1.00e-06 . .00002 15126 5 2001 0 1 "20010531" "816658108" "SMNS" 18699.84 2.00e-06 . .000021 15126 5 2001 0 1 "20010531" "516540101" "LNTE" 19084.29 2.00e-06 . .000021 15126 5 2001 0 1 "20010531" "871045100" "SWBD" 20032.32 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "784872301" "SVI" 20048.6 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "581243102" "MCKC" 20577.87 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "638588103" "NRI" 20597.52 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "170404107" "CHRD" 20771.1 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "278668108" "EBNX" 20801.2 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "03782R108" "APPS" 20880.8 2.00e-06 . .000023 15126 5 2001 0 1 "20010531" "83545R108" "SOFO" 21019.68 2.00e-06 . .000024 15126 5 2001 0 1 "20010531" "816288104" "SLTC" 21174.03 2.00e-06 . .000024 15126 5 2001 0 1 "20010531" "00437W102" "ACRU" 21392.8 2.00e-06 . .000024 15126 5 2001 0 1 "20010531" "591002100" "METG" 21640 2.00e-06 . .000024 15126 5 2001 0 1 "20010531" "262506108" "DSLN" 21800.84 2.00e-06 . .000025 15126 5 2001 0 1 "20010531" "232565101" "CYLK" 21810.36 2.00e-06 . .000025 15126 5 2001 0 1 "20010531" "63935M109" "NAVI" 21916.38 2.00e-06 2.00e-06 . 15126 5 2001 1 0 "20010531" "315653105" "FTGX" 21948.4 2.00e-06 . .000025 15126 5 2001 0 1 "20010531" "45677R107" "INFT" 22126.3 2.00e-06 . .000025 15126 5 2001 0 1 "20010531" "743663304" "POI" 22314.15 2.00e-06 . .000025 15126 5 2001 0 1 "20010531" "64107U101" "NETP" 22486.55 2.00e-06 . .000025 15126 5 2001 0 1 end format %td dt
I want to create a new variable PROD such that: for every (cusip yr) combination it takes the two observation i) when mn==5 ii) when mn==6 and multiples the r1000 of i) and r2000 of ii) to give PROD. So prod will be a 0/1 since r1000 and r2000 are bytes.
I am unable to pick the two obervations for multipication. It seems a little complicated.
So for example for cusip== 216485102 and year ==2001 it will take the observation where mn==5 and multiply its r1000 with the observation where mn==6 to give me PROD. Now I can set a loop for all cusip and year combination in the dataset.
One way I can think is break it into two datasets for each year, one where mn==5 and one where mn==6. Then merge both of them and thus essentially I will get the columns of the new dataset where mn==5 and mn==6 and then can simply multiple. Finally I can append all the years. But it seems too long and unncessairly complicated.
Thanks a lot!
Comment