Announcement

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

  • Zero Observations String Variable

    Hi,

    I try to import data from an xls file, containing both numeric as well as alphabetic values. Please see how my data looks like. When I use the command 'summarize', however, the variable Country says zero observations. This, even if it states that Country has a "str3" storage type.
    How should I proceed?

    Date Country NPL_Ratio GDP_Growth Unemployment_rate Inflation_Rate Interest_Rate Private_Debt_Change Stock_Return
    1999 AUT -1,769551079 -0,003596191 0,047 0,005689938 0,038426 -0,095191797
    2000 AUT -1,619788758 -0,09241522 0,0469 0,023448629 0,057533 0,051618 -0,008361147
    2001 AUT -1,619788758 0,000673837 0,0401 0,026500008 0,050996 0,016779 0,001044988
    2002 AUT -1,522878745 0,084719761 0,0485 0,018103579 0,050646 0,003698 0,013364127
    2003 AUT -1,522878745 0,223881294 0,0478 0,013555537 0,042233 0,029472 0,112757295

  • #2
    Stata does not allow the use of the -summarize- command on string variables; you did not supply a data example using -dataex- (please read the FAQ), but it appears that the -destring- command is what you want; see
    Code:
    h destring

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      Stata does not allow the use of the -summarize- command on string variables; you did not supply a data example using -dataex- (please read the FAQ), but it appears that the -destring- command is what you want; see
      Code:
      h destring
      I do not want to destring the variable. I want to keep it as string since it contains categorical data. However, I wonder what the issue is since it does not show any number of observations.

      Please see dataex below:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int Date str3 Country double(NPL_Ratio GDP_Growth Unemployment_rate Inflation_Rate Interest_Rate Private_Debt_Change Stock_Return)
      14245 "AUT"   -1.76955107862173    -.00359619144939654                .047   .00568993765885587               .038426                      .  -.09519179672544546
      14610 "AUT" -1.6197887582883939      -.092415220140246 .046900000000000004  .023448628535727697               .057533     .05161809928247165 -.008361146817868263
      14976 "AUT" -1.6197887582883939   .0006738373772020043               .0401    .0265000077279529               .050996    .016779224396017554  .001044987891582142
      15341 "AUT" -1.5228787452803376     .08471976102475824 .048499999999999995    .0181035787764132   .050646000000000004   .0036976170912080895  .013364126523785477
      15706 "AUT" -1.5228787452803376     .22388129367120513               .0478    .0135555370896445               .042233    .029471960704052325   .11275729455391012
      16071 "AUT" -1.5686362358410126     .14916783622162558               .0583  .020612061803286598               .042664   -.014512922465208767   .43314089161075725
      16437 "AUT"  -1.585026652029182    .049601587675601765 .056299999999999996    .0229913785639285               .035641     .03160513751597063   .45650117592707196
      16802 "AUT" -1.5686362358410126     .06333572582659588               .0524    .0144154851119019               .033271   -.006388110292679767   .30447304254722934
      17167 "AUT" -1.6575773191777938     .15739909247155795 .048600000000000004    .0216855528800755                 .0406    .030505805943712005    .1919113351728225
      17532 "AUT" -1.7212463990471711      .1098936107350134 .041299999999999996    .0321595033239883               .042169    .012286732874967945   -.2805998574329867
      17898 "AUT" -1.6382721639824072     -.0710223261304846                .053   .00506308827696486               .039981    .006917803911703883   -.3680588511991074
      18263 "AUT"  -1.548103086646509   -.021748068264216106               .0482    .0181353438995065               .037512     .02598213728061948    .2054538423087556
      18628 "AUT" -1.5675406232859719     .09937467364586915 .045599999999999995    .0328657914875376               .035407   -.011322822182991521  .008655582050343101
      18993 "AUT" -1.5507746915781435    -.05085158094479031               .0487    .0248567562177017               .032696    .002093467151037541  -.13389542816988592
      19359 "AUT"  -1.542641993656244     .05012925321180196 .053399999999999996    .0200015616900603                .01923    .030107526881720525   .12710629255427333
      19724 "AUT" -1.4592489510977256     .02906110456372313               .0562    .0160581182954471               .021311   -.027139874739039723  -.01157841843103307
      20089 "AUT" -1.4694366203889289    -.13708863132738044               .0572   .00896563335260302               .005434   -.006866952789699631  .008683961965352038
      20454 "AUT" -1.5694057793134415     .03591005471060967               .0601   .00891591752655335               .007672     .00802568218298563 -.030426199999999994
      20820 "AUT" -1.6254532720610406     .05404217037965471                .055    .0208126911385587               .005705  -.0008574228319451664   .33848191855019183
      21185 "AUT" -1.7252811644919177     .09130357648029076 .048499999999999995    .0199837981429546               .006692    .009317150913326041   .07442764708647295
      21550 "AUT"  -1.788180236228491   -.023128396873448476               .0449    .0153089564152645                .00452   -.008198712498481761  -.06729750004661685
      21915 "AUT"  -1.802228631052083    -.02202307329030517               .0536    .0138191063351859 -.0008680000000000001     .06741779437878873    -.183444956209487
      22281 "AUT"                   .      .1053424301584126 .061799999999999994  .027666666666666503              -.003995  -.0029256539697108093    .3740453774137473
      14245 "BEL" -1.5686362358410126 -.00010961905015736383  .08650000000000001    .0112084823417844                  .039                      .    .0334180823319908
      14610 "BEL" -1.5528419686577808     -.0835705996131528               .0659    .0254451776190906                 .0579    .057005377865836405  -.09757991143664953
      14976 "BEL"  -1.537602002101044  -.0006289653309244102 .061799999999999994  .024692582308607797                 .0516    .032401456726649645  -.06020891084733814
      15341 "BEL" -1.5228787452803376      .0906111139270448               .0691    .0164521436175359                 .0508   -.007625667894381987  -.10927095942222793
      15706 "BEL"  -1.585026652029182     .23161890007745933               .0768    .0158896399970379   .042800000000000005    .024359644537375835  -.15111808677974006
      16071 "BEL" -1.6382721639824072     .16051055961560232               .0736    .0209728311239314                 .0426    .018677281077770957   .31044085683074624
      16437 "BEL" -1.6989700043360187     .04573943156291746  .08439999999999999  .027814326367002498                 .0359   -.005610660254483457    .2932505371910241
      16802 "BEL" -1.7695510786217261    .057922871423870315               .0825    .0179120770070454                 .0337    .022619647355163697   .24091113871409636
      17167 "BEL"  -1.853871964321762     .15356335465035112               .0746     .018230563002681                 .0406     .05507660475885496    .1360808969822687
      17532 "BEL" -1.7695510786217261     .09828909205950172               .0698    .0448944420508401                 .0425     .10575710883877303  -.26999334745276976
      17898 "BEL" -1.5086383061657274    -.06677363852533492               .0791 -.000531456741253762   .041299999999999996    .036230048137826334  -.30488838833201937
      18263 "BEL" -1.5530576183787825  -.0018975410685062044  .08289999999999999    .0218929920422458                 .0375    -.05203748981255108   .19636642335793472
      18628 "BEL" -1.4811834754625894     .08597716131121151  .07139999999999999  .035320821072274404                 .0414     .05072432618320953   -.0359163815140629
      18993 "BEL" -1.4273316580160689    -.05116591079793375               .0754    .0283966343445897   .041100000000000005    .016814629955406328  .030215528887748762
      19359 "BEL" -1.3721243515708175     .05102453939536767               .0843    .0111309594027537                 .0231 -.00024140983342718837   .15931543968044995
      19724 "BEL"  -1.379271750495911    .026312813937933957               .0852   .00340002833356965                 .0245 -.00032195750160975205   .18072386651799777
      20089 "BEL" -1.4214352796422318    -.13658644069695572               .0848   .00561429152790106                 .0073   .0010064412238325066    .1384563260830363
      20454 "BEL"  -1.464448924559488    .029300385178520835               .0783    .0197385264653172                 .0086       .097124472149608  -.04767629999999997
      20820 "BEL"   -1.53390531081779     .05600811882394717               .0709    .0212597086002609   .006999999999999999   -.049376832844574954     .065956879997841
      21185 "BEL" -1.6443052511323342     .08149633794745985 .059500000000000004    .0205316499865181   .006999999999999999    .003933212509158467 -.048880741102669927
      21550 "BEL" -1.6804629845215553   -.014085284410151118               .0536    .0143681956996435                 .0077   -.004109852122143583 -.040049283522562186
      21915 "BEL" -1.6840384474823311   -.020711340013808803               .0555  .007407918122203851                -.0003     .03675563097809333  -.15977746925275282
      22281 "BEL" -1.6922343933812187     .13283192737680333               .0626  .024402485114408103                -.0036    -.03958186079386921   .17724545715683926
      14245 "HRV"  -.9871627752948278     -.0833636953541459               .1356  .040191387559808396                     .                      .  -.13307361486184668
      14610 "HRV" -1.0222763947111522   -.061347352355110865               .1606  .046113155473780004                     .                      .   .25540831368563743
      14976 "HRV"  -1.136677139879544     .04164408310749774               .1582     .037767024666589                     .                      .    .1404953606418744
      15341 "HRV"  -.9913998282380825      .1595698551730119               .1505    .0167178437393382                     .                      .   .20166073821593483
      15706 "HRV" -1.0506099933550872     .31803903971281133               .1392    .0176733780760626                     .                      .   -.0325049292236281
      16071 "HRV"    -1.1249387366083     .18696626663262123               .1366    .0205539679050332                     .                      .   .13782055407730492
      16437 "HRV"  -1.207608310501746      .0768697564356909                .126    .0331717824448043                     .                      .    .4951505108609809
      16802 "HRV" -1.2839966563652008     .09943843917164229  .11130000000000001     .031898259147295                 .0403                      .    .4428625989266471
      17167 "HRV" -1.3187587626244128     .19475904952254053  .09910000000000001  .028992827558339197                 .0475                      .    .6865153759761572
      17532 "HRV" -1.3098039199714864      .1547144305508618  .08529999999999999    .0607696838798351                 .0555                      .  -.29408300385454106
      17898 "HRV" -1.1135092748275182     -.0945914305783189                .092    .0237852845904674                 .0683                      .   -.4328585803976556
      18263 "HRV"  -.9549783930986224   -.050604473752262735               .1162    .0103055505333573   .059500000000000004                      .   .06337455180231404
      18628 "HRV"  -.9110528044135227     .06549088370984492               .1368  .022727272727272697                 .0655                      .  .045112716579558086
      18993 "HRV"  -.8613941911236018    -.08501629705374832               .1593    .0341207349081366                 .0754                      .  -.16399427278800577
      19359 "HRV"  -.8116228156386591     .03665287163636999               .1725  .022165820642977997                 .0429                      .   .06809286440042073
      19724 "HRV"   -.777124923306181  -.0022740289054340866               .1729  -.00215196159576215   .051100000000000007                      .  -.03359865534568105
      20089 "HRV"  -.7870058368709365    -.14326957167699347               .1618  -.00464499004645002                 .0332                      .  -.03588996033651297
      20454 "HRV"   -.866161021191456    .032141097645088185                .131   -.0112500000000001                 .0384                      .   .03331199999999995
      20820 "HRV"  -.9507238167848703     .06766457919657859               .1121    .0112937210282344   .027999999999999997                      .   .07999423213898611
      21185 "HRV" -1.0129185496178397     .09722818311874105               .0843  .015001250104175301                 .0235                      .  -.06368445058160113
      21550 "HRV" -1.1555458132688121  -.0003259611780236974               .0662   .00771820346498083                 .0223                      .   .04577084336219414
      21915 "HRV" -1.1976073585890323    -.06126807636499992               .0751 .0015481137456202802  .0060999999999999995                      .  -.11440799028485618
      22281 "HRV"  -1.244183746305383     .19646051511835913               .0761  .025545069964204598                 .0058                      .   .17807364456077407
      14245 "CYP"                   .     .02472356386657607                .057    .0162999999999998                     .                      .                    .
      14610 "CYP"                   .    -.04894305846505428 .049699999999999994    .0414165764702056                     .                      .                    .
      14976 "CYP"                   .     .04094913896676011               .0398    .0197304191729919                     .                      .                    .
      15341 "CYP"                   .     .09858613061460036                .033    .0280115505180746                     .                      .                    .
      15706 "CYP"                   .     .27403256872701803               .0414  .041390653868685595                     .                      .                    .
      16071 "CYP"                   .     .19076415612974162               .0433  .022862170680167503                     .                      .                    .
      16437 "CYP"                   .     .07992843951985226                .053  .025601613233023502                     .                      .                    .
      16802 "CYP"                   .     .09132688505317159               .0454  .023046873297855697                     .                      .                    .
      17167 "CYP"                   .      .1792184898638723               .0392  .023726518366972198                     .                      .                    .
      17532 "CYP" -1.4436974992327127      .1608255128311602               .0365   .04669008419024841                     .                      .                    .
      17898 "CYP" -1.3467874862246563     -.0692183866928993               .0536   .00326276652249795                     .                      .                    .
      18263 "CYP"  -1.234948686114516   -.007647963105303613               .0626  .024300408410225297                     .                      .                    .
      18628 "CYP" -1.0005984356191289     .07033035126447465               .0786  .032894493956421196                     .                      .                    .
      18993 "CYP"  -.7357750157575833    -.09313601331548287  .11800000000000001    .0238905410042319                     .                      .                    .
      19359 "CYP" -.41389676455476304    -.04400909707537007  .15869999999999998  -.00399357676464408                     .                      .                    .
      19724 "CYP" -.34705683230935336   -.030383973288814693               .1609 -.013549888544631199                     .                      .                    .
      20089 "CYP" -.32104663392046906    -.14294938016528927               .1491   -.0209699769053118                     .                      .                    .
      20454 "CYP" -.43534458615190014      .0567023253477977               .1295   -.0142916666666668                     .                      .                    .
      20820 "CYP"  -.5032239035513348     .09020912547528517               .1105     .005317664961745                     .                      .                    .
      21185 "CYP"  -.7095090624654381      .1164443281890313               .0837    .0143549119531762                     .                      .                    .
      21550 "CYP"  -.7671791666061767    .013237533679565778               .0707 .0025037099675845596                     .                      .                    .
      21915 "CYP"  -.8233565669165341    -.03699707106520734               .0759  -.00638422797978907                     .                      .                    .
      22281 "CYP" -1.0458403798561435     .13762606050904433               .0746    .0244608867175472                -.0019                      .                    .
      14245 "EST" -1.7695510786217261    .017257325900497496               .1157    .0329611089718906                     .                      .  -.35852720170127134
      14610 "EST"                  -2    -.01126126126126126               .1336     .040184895250876                     .    .015479072985126276    .3164968594186586
      14976 "EST" -1.8860566476931633     .09847555633432627               .1313    .0574827981651377                     .     .06650770671889622  -.08414814973041197
      15341 "EST" -2.0969100130080562     .17897591322379966               .1003    .0357191270164023                     .     .09206323858192285   .33696267646239475
      15706 "EST" -2.3979400086720375     .33824922202678936  .11289999999999999    .0133499116549964                     .    -.03385245302332399    .3965595776786865
      16071 "EST" -2.5228787452803374      .2291982610453948               .1025    .0304811107523412                     .     .07582866656576363    .4148200606592855
      16437 "EST" -2.6989700043360187     .16186872840927785               .0803  .040797142319984596                     .     .09728474957794041    .7225144757462273
      16802 "EST" -2.6989700043360187     .20678182075605267               .0591    .0443762042389211                     .     .10449387781268017   .11102914899403028
      end
      format %td Date

      Comment


      • #4
        in Stata, one cannot do arithmetic or algebra or ... on string variables; you can -tabulate- such variables; if you need to use a variable such as country in your data above, you will need to -encode- it; I strongly recommend setting up your labels first and then encoding; other than that, I don't follow what you want to do - maybe someone else can help; see
        Code:
        h encode

        Comment


        • #5
          The primary role of summarize is to provide summary statistics such as mean and SD, depending on options.

          If you have a string variable that role is impossible to fulfil, but as a courtesy to users summarize doesn't throw you out if you ask for summarize results from string variables; it just shows 0 observations, which in this context (and several others in Stata) means there are no observations on which Stata can do what you ask with that variable.

          You'd get the same result if you fed one or more string variables to (say) regress, even if you also fed some numeric variables at the same time.

          Comment


          • #6
            there are no observations on which Stata can do what you ask with that variable.
            This behavior is (perhaps surprisingly) entirely consistent with the rest of the language, where Stata routinely drops a subset of observations when they can't be used to preform an operation - usually when they contain missing data. The zero is just what you see when all of the cases are dropped. Some might call this a "silent error" but most of the time I'd rather the command completed execution (rather than halting on an error) because, for the initiated, partial results can be more informative about the underlying problem than a generic error message.

            That said, this kind of thing confused the hell out of me more than once when I started learning the language a few years ago. I sympathize with the OP.
            Last edited by Daniel Schaefer; 12 Jun 2023, 15:02.

            Comment


            • #7
              Sky Emanuel I'm not sure what exactly you were hoping to achieve by using the summarize command on a string variable, but you might want to look into commands such as codebook or tabulate

              For your data example, these commands would produce the following:

              Code:
              . codebook Country
              
              ------------------------------------------------------------------------------------------------------------------
              Country                                                                                                (unlabeled)
              ------------------------------------------------------------------------------------------------------------------
              
                                Type: String (str3)
              
                       Unique values: 5                         Missing "": 0/100
              
                          Tabulation: Freq.  Value
                                         23  "AUT"
                                         23  "BEL"
                                         23  "CYP"
                                          8  "EST"
                                         23  "HRV"
              
              . tabulate Country
              
                  Country |      Freq.     Percent        Cum.
              ------------+-----------------------------------
                      AUT |         23       23.00       23.00
                      BEL |         23       23.00       46.00
                      CYP |         23       23.00       69.00
                      EST |          8        8.00       77.00
                      HRV |         23       23.00      100.00
              ------------+-----------------------------------
                    Total |        100      100.00

              Comment


              • #8
                Thank you all for your help. I just wanted the number of observations of countries to be visible next to my numeric data.
                Also, thanks Hemanshu Kumar , these commands gave me what I wanted.

                Comment

                Working...
                X