Announcement

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

  • Create additional varaibles based on values of an existing variable

    Dear Statalisters,

    My request is simple, but I give a lot of explanation to make the data clear. Thanks for your patience !

    Below I present a few observation of a panel data set. The data is organized by
    • firm and segment
    data have
    Code:
    clear
    input int(firmid segmentid) str4 primaryid byte segsale
    1000 3100 "3100" 40 
    1000 3200 "." 10 
    1000 3300 "." 0 
    2000 3100 "3100" 15 
    2000 3200 "." 0 
    2000 3300 "." 0 
    1200 3200 "." 0 
    1200 3300 "." 12 
    1200 3100 "3100" 49 
    end

    the three firms have a common trait – their primaryid is the same 3100, primaryid denotes the segment a firm has the largest sales. E.g. firm 1000 has 2 segments 3100, 3200, the largest sales is in 3100. (think about a gas station, selling snacks, and gas, as two distinct segments)
    My request: (you shall see the data I want below)
    1. I want to generate 2 new variables sid1 sid2 that takes values of all non-primaryid, in this case, 3200, 3300,
    2. Next, I want to merge these variables back to the main dataset. ( I know how to merge ,but not sure how to do 1 above)
    I do not want to do this manually, given that my real data has over 1000 segments, and several hundred primaryid groups.

    data want

    Code:
    clear
    input int(firmid segmentid) str4 primaryid byte segsale int(sid1 sid2)
    1000 3100 "3100" 40 . . 
    1000 3200 "." 10 3200 . 
    1000 3300 "." 0 . . 
    2000 3100 "3100" 15 . . 
    2000 3200 "." 0 . . 
    2000 3300 "." 0 . . 
    1200 3200 "." 0 . . 
    1200 3300 "." 12 . 3300 
    1200 3100 "3100" 49 . . 
    end
    You may notice I have 3 firmid, each firmid corresponds to 3 segmentid. Only segments with non-zero sales are real segment id, that is, the firm does operate in the segment. I processed the data to include all possible segments for all firms, for future analysis.



    Thank you,
    Rochelle

  • #2
    I'm confused by your data and explanation. In your example, each firm has non-zero sales only in one other segment. For firm 1000, that is segment 3200, and for firm 1200 it is 3300. Why does firm 1000's segment 3200 go in sid1 but firm 1200's segment 3300 go in sid2. What is the difference between the two sid variables, and how do you decide which variable gets the segment when there is only 1?

    Also, do I understand correctly that, after merging back to the original data, you want these variables to be populated only in those observations where an sid variable matches the segment id, and you want missing values elsewhere?


    Comment


    • #3
      Hi Clyde,

      Thank you for extending your help again !

      Here is my answer to questions you raised in #2.

      The three firms have a common trait - share the same primaryid 3100, e.g. if we have 3 firms all sell light bulbs in their primary business. but each firm could also have other lines of business. For simple illustration, I list a second segment with positive sales for firmid 1000 and 1200. firm 2000 has only 1 line of business.

      my analysis want to compare these three firms segment sales. To do so, I need to create some kind of total sales for each segment (3100, 3200, 3300).This is the reason I try to create sid1 and sid2 variables. Later on I can

      bysort sid1: egen sale_sid1=total(sale)


      your second question about merging :
      • for sid1=3200, among the three firms, only firmid1000 has nonzero sales in this segment, so I want to "flag" or "merge to this firm's corresponding segment
      • for sid2=3300, among the three firms, only firmid1200 has nonzero sales in this segment, so I want to "flag" or "merge to this firm's corresponding segment
      so after merge, I will total sales in sic1 and sic2 and get the following results

      Code:
      clear
      input int(gvkey sic) str4 psic byte segsale int(totsale_psic sic1) byte totsale_sic1 int sic2 byte totsale_sic2
      1000 3100 "3100" 40 104 . . . . 
      1000 3200 "." 10 . 3200 10 . . 
      1000 3300 "." 0 . . . 3300 12 
      2000 3100 "3100" 15 104 . . . . 
      2000 3200 "." 0 . 3200 10 . . 
      2000 3300 "." 0 . . . 3300 12 
      1200 3200 "." 0 . 3200 10 . . 
      1200 3300 "." 12 . . . 3300 12 
      1200 3100 "3100" 49 104 . . . . 
      end
      i got totsale_psic sic1 by doing
      Code:
      bysort sic1: egen totsale_psic=total(segsale)

      What i stated above is the only way I came up with that can allow me to compare by individual segment the three firms sales :
      that is , I will be focusing on totsale_psic, totsale_sic1, totsale_sic2 across 3 firms, and do it segment by segment.

      Best,

      Rochelle

      Comment


      • #4
        So, I think the following does it. It reproduces your example results.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int(gvkey sic) str4 psic byte segsale
        1000 3100 "3100" 40
        1000 3200 "."    10
        1000 3300 "."     0
        2000 3100 "3100" 15
        2000 3200 "."     0
        2000 3300 "."     0
        1200 3200 "."     0
        1200 3300 "."    12
        1200 3100 "3100" 49
        end
        
        
        destring psic, replace // NEED THIS TO BE NUMERIC
        
        // COPY ORIGINAL DATA TO A TEMPFILE
        tempfile holding
        save `holding'
        
        keep gvkey sic psic segsale
        // VERIFY EACH GVKEY HAS ONLY ONE NON-MISSINSG VALUE OF PSIC
        // AND SPREAD THAT TO ALL OBSERVATIONS OF THAT GVKEY
        by gvkey (psic), sort: assert psic == psic[1] if !missing(psic)
        by gvkey (psic): replace psic = psic[1]
        
        // CALCULATE EACH FIRM'S TOTAL SALES IN EACH SEGMENT
        egen tot_sales_sic = total(segsale), by(psic sic)
        // ASSIGN SEQUENCE NUMBERS TO EACH SEGMENT WITHIN A FIRM
        by gvkey sic, sort: gen int seq = 1 if _n == 1 & sic != psic
        by gvkey (sic): replace seq = sum(seq)
        
        // RESHAPE WIDE
        drop segsale
        reshape wide tot_sales_sic sic, i(gvkey) j(seq)
        
        // MERGE BACK THE ORIGINAL DATA
        merge 1:m gvkey using `holding', assert(match) nogenerate
        sort gvkey sic
        order sic psic, after(gvkey)
        
        forvalues i = 0/2 {
            replace tot_sales_sic`i' = . if sic != sic`i'
            replace sic`i' = . if sic != sic`i'
        }
        rename tot_sales_sic0 tot_sales_psic
        drop sic?*
        But I am confused by your final remark
        I will be focusing on totsale_psic, totsale_sic1, totsale_sic2 across 3 firms, and do it segment by segment.
        because the totsale variables in your example are totals over the entire psic, not just within firms (gvkey). (For example, for gvkey 2000, the firm level total sales for segments 3200 and 3300 are both zero, not 10 and 12.) It is not hard to modify this code to give you totals of sales within firms by sic--but that is not what you put in your example.

        Comment


        • #5
          Dear Clyde,

          Your code works great !!! You have always been a great help

          Here is my answer to your post #2 confusion

          You are correct that

          Code:
           totsale variables in your example are totals over the entire psic,
          my analysis requires to compute the total sales for each segment across all firms sharing the same psic, that is I the data I posted, I need to total (40+15+49) for psic 3100,, total (10+0+0) for 3200, and (0+0+12) for 3300, because 2 out of the three firms have zero sales for non psic segments, so it is not clear to readers, why I give 104 for psic.


          one question I have about your code :

          Code:
           
           forvalues i = 0/2 {     replace tot_sales_sic`i' = . if sic != sic`i'     replace sic`i' = . if sic != sic`i' }
          my real data has many different psic, and many different sic, do I need to do a count to know how many different sic I have, e.g. it is 15 , then set

          Code:
            
           forvalues i = 0/15 {     replace tot_sales_sic`i' = . if sic != sic`i'     replace sic`i' = . if sic != sic`i' }
          currently i am applying your code to my real data.



          Sincerely,
          Rochelle




          Comment


          • #6
            Yes, you will need to get an actual count of the number of sic's. So I would modify the code this way:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int(gvkey sic) str4 psic byte segsale
            1000 3100 "3100" 40
            1000 3200 "."    10
            1000 3300 "."     0
            2000 3100 "3100" 15
            2000 3200 "."     0
            2000 3300 "."     0
            1200 3200 "."     0
            1200 3300 "."    12
            1200 3100 "3100" 49
            end
            
            
            destring psic, replace // NEED THIS TO BE NUMERIC
            
            // COPY ORIGINAL DATA TO A TEMPFILE
            tempfile holding
            save `holding'
            
            keep gvkey sic psic segsale
            // VERIFY EACH GVKEY HAS ONLY ONE NON-MISSINSG VALUE OF PSIC
            // AND SPREAD THAT TO ALL OBSERVATIONS OF THAT GVKEY
            by gvkey (psic), sort: assert psic == psic[1] if !missing(psic)
            by gvkey (psic): replace psic = psic[1]
            
            // CALCULATE EACH FIRM'S TOTAL SALES IN EACH SEGMENT
            egen tot_sales_sic = total(segsale), by(psic sic)
            // ASSIGN SEQUENCE NUMBERS TO EACH SEGMENT WITHIN A FIRM
            by gvkey sic, sort: gen int seq = 1 if _n == 1 & sic != psic
            by gvkey (sic): replace seq = sum(seq)
            summ seq, meanonly
            local seq_max = r(max)
            
            // RESHAPE WIDE
            drop segsale
            reshape wide tot_sales_sic sic, i(gvkey) j(seq)
            
            // MERGE BACK THE ORIGINAL DATA
            merge 1:m gvkey using `holding', assert(match) nogenerate
            sort gvkey sic
            order sic psic, after(gvkey)
            
            forvalues i = 0/`seq_max' {
                replace tot_sales_sic`i' = . if sic != sic`i'
                replace sic`i' = . if sic != sic`i'
            }
            rename tot_sales_sic0 tot_sales_psic
            drop sic?*

            Comment


            • #7
              Hi Clyde,

              My data has multiple years. I just run the code for 1 year which I have 3 million observations. I got an error,


              Code:
              . // VERIFY EACH GVKEY HAS ONLY ONE NON-MISSINSG VALUE OF PSIC
              . // AND SPREAD THAT TO ALL OBSERVATIONS OF THAT GVKEY
              . by gvkey (psic), sort: assert psic == psic[1] if !missing(psic)
              6 contradictions in 5,325 observations
              assertion is false
              how should I list those 6 contradictions to pin down my error ?


              Rochelle

              Comment


              • #8
                So you have some GVKEY's for which different psic's are specified in different observations of that firm. To list them:

                Code:
                by gvkey (psic), sort: gen byte problem = (psic != psic[1] & !missing(psic))
                list if problem

                Comment


                • #9
                  Using your code, I found out 2 firms causing problems, both firms have multiple segments, and 2 segments report same sales, so I coded both as primary segment. I now keep one and the code is fine.

                  Thanks !

                  I got an error, and want to ask if it is caused by my real data has some firms with multiple segments, reporting same sic for these segments.


                  Code:
                   reshape wide tot_sales_sic sic, i(gvkey) j(seq)
                  (note: j = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
                  >  32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 
                  > 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 9
                  > 4 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1
                  > 19 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                  >  143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1
                  > 66 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                  >  190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 2
                  > 13 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                  >  237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 2
                  > 60 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                  >  284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 3
                  > 07 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                  >  331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 3
                  > 54 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                  >  378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 4
                  > 01 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
                  >  425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 4
                  > 48 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                  >  472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 4
                  > 95 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
                  >  519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 5
                  > 42 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
                  >  566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 5
                  > 89 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
                  >  613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 6
                  > 36 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
                  >  660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 6
                  > 83 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
                  >  707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 7
                  > 30 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
                  >  754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 7
                  > 77 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
                  >  801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 8
                  > 24 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
                  >  848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 8
                  > 71 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
                  >  895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 9
                  > 18 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
                  >  942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 9
                  > 65 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
                  >  989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
                  >  1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 102
                  > 8 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 10
                  > 47 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1
                  > 066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 
                  > 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
                  >  1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 112
                  > 2 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 11
                  > 41 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1
                  > 160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 
                  > 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
                  >  1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 121
                  > 6 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 12
                  > 35 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1
                  > 254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 
                  > 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
                  >  1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 131
                  > 0)
                  values of variable seq not unique within gvkey
                      Your data are currently long.  You are performing a reshape wide.  You specified i(gvkey)
                      and j(seq).  There are observations within i(gvkey) with the same value of j(seq).  In the
                      long data, variables i() and j() together must uniquely identify the observations.
                  
                           long                                wide
                          +---------------+                   +------------------+
                          | i   j   a   b |                   | i   a1 a2  b1 b2 |
                          |---------------| <--- reshape ---> |------------------|
                          | 1   1   1   2 |                   | 1   1   3   2  4 |
                          | 1   2   3   4 |                   | 2   5   7   6  8 |
                          | 2   1   5   6 |                   +------------------+
                          | 2   2   7   8 |
                          +---------------+
                      Type reshape error for a list of the problem variables.

                  Comment


                  • #10
                    I got an error, and want to ask if it is caused by my real data has some firms with multiple segments, reporting same sic for these segments.
                    Yes, that's exactly what it is! Either you need to re-code some of these by giving them different sic numbers, or if the sic number is more "real" than the firm's reported segmentation, you might want to reduce each sic number (within a firm) to a single observation by, say, adding up the sales (which you could do with -collapse-.) Either approach will resolve the Stata problem--which is better in your situation depends on the substantive issues here and I lack the expertise to comment on those.

                    Comment


                    • #11
                      Hi Clyde,
                      In response to your post #10, I thought my error from #9 maybe caused by

                      Code:
                      input int(firmid sic) str4 psic byte segsale
                      1000 3100 "3100" 40
                      1000 3200 "3100"    10
                      1000 3200 "3100"    15
                      1000 3300 "3100"     0
                      2000 3100 "3100" 15
                      2000 3200 "3100"     0
                      2000 3300 "3100"     0
                      1200 3200 "3100"     0
                      1200 3300 "3100"    12
                      1200 3100 "3100" 49
                      end
                      above for firmid 1000, it has 2 segments with same sic 3200, I use


                      Code:
                      keep gvkey sic psic segsale
                      * collapse sic if duplciate records
                      
                      collapse (sum) segsale=segsale, by(gvkey sic) 
                      
                      * this above adds up sales, which means I need to redefine psic seg 
                      * because combining segments increase sales, which could make the new segment the primary segment - with largest sales within a firm
                      
                      *drop psic
                      
                      bysort gvkey : egen maxsale=max(segsale)
                      
                      *define new psic segment
                      gen psic=sic if segsale==maxsale
                      
                      drop maxsale
                      in my real data , my reshape did not work, produce same error in my post #9, i wonder if I need to modify code with respect to
                      Code:
                      // CALCULATE EACH FIRM'S TOTAL SALES IN EACH SEGMENT
                      egen tot_sales_sic = total(segsale), by(psic sic)
                      // ASSIGN SEQUENCE NUMBERS TO EACH SEGMENT WITHIN A FIRM
                      by gvkey sic, sort: gen int seq = 1 if _n == 1 & sic != psic
                      by gvkey (sic): replace seq = sum(seq)
                      could it be the seq number assigned here in my real data has duplicates, i.e., 2 segments with same seq, that is causing reshape failure?

                      Comment


                      • #12
                        Oh, I think I see the problem. When you run
                        Code:
                        bysort gvkey : egen maxsale=max(segsale)
                        gen psic=sic if segsale==maxsale
                        if there is a firm that has two segments tied for the largest amount of sales, then that firm will have two different psic's. When you try to -reshape wide-, Stata will notice that and complain and halt. I think that's what's going wrong here.

                        In the code originally posted in #6, there were a couple of lines of code that tested for that, but the code noted here has been inserted after that test was passed, and I believe it is introducing new violations that weren't originally there.

                        If this is what's happening, then Stata is actually doing you a favor by making you realize (indirectly) that you have not adequately specified the definition of psic. If you hadn't done the -reshape-, the problem would crop up somewhere else, possibly as results that change each time you run it.

                        So I think what you need to do is after those two lines of code above, insert:
                        Code:
                        by gvkey (psic), sort: assert psic == psic[1] if !missing(psic)
                        If that test fails (you get an error message), that will confirm my theory of what is going wrong. The solution then is to figure out some way to break the ties so that you only have one psic for each firm.

                        Comment


                        • #13
                          Actually the data passed the assert test , see log below.


                          Code:
                          . // VERIFY EACH GVKEY HAS ONLY ONE NON-MISSINSG VALUE OF PSIC
                          . // AND SPREAD THAT TO ALL OBSERVATIONS OF THAT GVKEY
                          . by firmid (psic), sort: assert psic == psic[1] if !missing(psic)
                          
                          . by firmid (psic): replace psic = psic[1]
                          (3,272,378 real changes made)
                          I posted my code here and error afterwards.

                          Code:
                          use segsale,clear
                          
                          
                          * my data structure is that if a Mul segment firms, psic is filled for all x segments 
                          *drop if psic==.
                          
                          
                          drop year 
                          
                          destring firmid, replace
                          
                          *keep if firmid==1004
                          rename sale segsale
                          
                          rename sics sic
                          
                          tempfile holding
                          save `holding'
                          
                          keep firmid sic psic segsale
                          
                          
                          * collapse sic if duplciate records
                          
                          collapse (sum) segsale=segsale, by(firmid sic) 
                          
                          * this above adds up sales, which means I need to redefine pri seg 
                          
                          *drop psic
                          
                          bysort firmid : egen maxsale=max(segsale)
                          
                          gen psic=sic if segsale==maxsale
                          
                          drop maxsale
                          
                          *by firmid (sic), sort: gen byte problem = (sic != sic[1] & !missing(sic))
                          *list if problem
                          Code:
                          // VERIFY EACH firmid HAS ONLY ONE NON-MISSINSG VALUE OF PSIC
                          // AND SPREAD THAT TO ALL OBSERVATIONS OF THAT firmid
                          by firmid (psic), sort: assert psic == psic[1] if !missing(psic)
                          by firmid (psic): replace psic = psic[1]
                          
                          // if you get an error from assert , then d o 
                          //So you have some firmid's for which different psic's are specified in different observations of that firm. To list them:
                          
                          //Code:
                          //by firmid (psic), sort: gen byte problem = (psic != psic[1] & !missing(psic))
                          //list if problem
                          
                          
                          
                          // CALCULATE EACH FIRM'S TOTAL SALES IN EACH SEGMENT
                          egen tot_sales_sic = total(segsale), by(psic sic)
                          // ASSIGN SEQUENCE NUMBERS TO EACH SEGMENT WITHIN A FIRM
                          by firmid sic, sort: gen int seq = 1 if _n == 1 & sic != psic
                          by firmid (sic): replace seq = sum(seq)
                          
                          * count # of segments
                          summ seq, meanonly
                          local seq_max = r(max)
                          
                          
                          // RESHAPE WIDE
                          drop segsale
                          reshape wide tot_sales_sic sic, i(firmid) j(seq)
                          
                          // MERGE BACK THE ORIGINAL DATA
                          merge 1:m firmid using `holding', assert(match) nogenerate
                          sort firmid sic
                          order sic psic, after(firmid)

                          Code:
                          values of variable seq not unique within gvkey
                              Your data are currently long.  You are performing a reshape
                              wide.  You specified i(gvkey) and j(seq).  There are
                              observations within i(gvkey) with the same value of j(seq).  In
                              the long data, variables i() and j() together must uniquely
                              identify the observations.
                          
                                   long                                wide
                                  +---------------+                   +------------------+
                                  | i   j   a   b |                   | i   a1 a2  b1 b2 |
                                  |---------------| <--- reshape ---> |------------------|
                                  | 1   1   1   2 |                   | 1   1   3   2  4 |
                                  | 1   2   3   4 |                   | 2   5   7   6  8 |
                                  | 2   1   5   6 |                   +------------------+
                                  | 2   2   7   8 |
                                  +---------------+
                              Type reshape error for a list of the problem variables.
                          r(9);
                          
                          end of do-file
                          
                          r(9);

                          Comment


                          • #14
                            It's a little difficult troubleshooting this without the data that produces the problem, but I see another point where things could have gone wrong. In the example data, the psic was always the lowest numbered sic. So when we get to:

                            Code:
                            by gvkey sic, sort: gen int seq = 1 if _n == 1 & sic != psic
                            by gvkey (sic): replace seq = sum(seq)
                            in the first command, the observation within gvkey first observation, the one where sic == psic, gets a missing value for seq. The others, which follow, each get a 1, and then the second command adds those up and we get 0, 1, 2, ...
                            But suppose that the psic is not the lowest numbered sic for a firm. Say it's the second one of three. Then the first command sets seq = 1, then ., then 1 and the second command converts that to 1, 1, 2, so that seq no longer unique. And, in fact, in your example data when I change a psic from 3100 to 3200, I get the -reshape error-.

                            So here's what I think these two lines should be replaced by:

                            Code:
                            isid gvkey sic
                            gen not_psic = (sic != psic) // 0 FOR PSIC, 1 FOR OTHERS
                            by gvkey (not_psic sic), sort: gen seq = _n-1 // SORT THE PSIC TO THE TOP OF THE LIST
                            drop not_psic
                            When I try this with the example data and psic changed from 3100 to 3200, it now passes the -reshape-. So I think that's the solution.

                            If this does not work for you in the real data, after the -reshape- fails, please run -reshape error- and post the output of that.​

                            Added: By the way, the -drop not_psic- is essential here. If you omit that, then not_psic will vary within gvkey and will abort the -reshape-. So don't leave it out! (I made that mistake at one point while working on this.)
                            Last edited by Clyde Schechter; 08 Jun 2016, 22:20.

                            Comment


                            • #15
                              Hi Clyde,

                              Following your advice in #14, I did not get reshape error. However, my computer froze in the process. I think it is due to adding 2600 new variables to the dataset. Currently I am restructuring my dataset to reduce the dimensions , and will come back to this part of programming (i.e. reshape).

                              Thanks so much for your support as always !

                              Rochelle

                              Comment

                              Working...
                              X