Announcement

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

  • Display local with blank space between each element

    Dear Stata users,

    I have an elementary question however for which I cannot find an answer. Suppose we have a local that is composed of many elements. When we display this local, how can we add blank space between each element? For example: AMC Concord AMC Pacer AMC Spirit Audi 5000 Audi Fox BMW 320.Thank you.

    Code:
    sysuse auto
    levelsof make, local(make_local)
    display `make_local'
    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . levelsof make, local(make_local)
    `"AMC Concord"' `"AMC Pacer"' `"AMC Spirit"' `"Audi 5000"' `"Audi Fox"' `"BMW 320i"' `"Buick Century"' `"Buick El
    > ectra"' `"Buick LeSabre"' `"Buick Opel"' `"Buick Regal"' `"Buick Riviera"' `"Buick Skylark"' `"Cad. Deville"' `
    > "Cad. Eldorado"' `"Cad. Seville"' `"Chev. Chevette"' `"Chev. Impala"' `"Chev. Malibu"' `"Chev. Monte Carlo"' `"
    > Chev. Monza"' `"Chev. Nova"' `"Datsun 200"' `"Datsun 210"' `"Datsun 510"' `"Datsun 810"' `"Dodge Colt"' `"Dodge
    >  Diplomat"' `"Dodge Magnum"' `"Dodge St. Regis"' `"Fiat Strada"' `"Ford Fiesta"' `"Ford Mustang"' `"Honda Accor
    > d"' `"Honda Civic"' `"Linc. Continental"' `"Linc. Mark V"' `"Linc. Versailles"' `"Mazda GLC"' `"Merc. Bobcat"'
    > `"Merc. Cougar"' `"Merc. Marquis"' `"Merc. Monarch"' `"Merc. XR-7"' `"Merc. Zephyr"' `"Olds 98"' `"Olds Cutl Su
    > pr"' `"Olds Cutlass"' `"Olds Delta 88"' `"Olds Omega"' `"Olds Starfire"' `"Olds Toronado"' `"Peugeot 604"' `"Pl
    > ym. Arrow"' `"Plym. Champ"' `"Plym. Horizon"' `"Plym. Sapporo"' `"Plym. Volare"' `"Pont. Catalina"' `"Pont. Fir
    > ebird"' `"Pont. Grand Prix"' `"Pont. Le Mans"' `"Pont. Phoenix"' `"Pont. Sunbird"' `"Renault Le Car"' `"Subaru"
    > ' `"Toyota Celica"' `"Toyota Corolla"' `"Toyota Corona"' `"VW Dasher"' `"VW Diesel"' `"VW Rabbit"' `"VW Scirocc
    > o"' `"Volvo 260"'
    
    . display `make_local'
    AMC ConcordAMC PacerAMC SpiritAudi 5000Audi FoxBMW 320iBuick CenturyBuick ElectraBuick LeSabreBuick OpelBuick Reg
    > alBuick RivieraBuick SkylarkCad. DevilleCad. EldoradoCad. SevilleChev. ChevetteChev. ImpalaChev. MalibuChev. Mo
    > nte CarloChev. MonzaChev. NovaDatsun 200Datsun 210Datsun 510Datsun 810Dodge ColtDodge DiplomatDodge MagnumDodge
    >  St. RegisFiat StradaFord FiestaFord MustangHonda AccordHonda CivicLinc. ContinentalLinc. Mark VLinc. Versaille
    > sMazda GLCMerc. BobcatMerc. CougarMerc. MarquisMerc. MonarchMerc. XR-7Merc. ZephyrOlds 98Olds Cutl SuprOlds Cut
    > lassOlds Delta 88Olds OmegaOlds StarfireOlds ToronadoPeugeot 604Plym. ArrowPlym. ChampPlym. HorizonPlym. Sappor
    > oPlym. VolarePont. CatalinaPont. FirebirdPont. Grand PrixPont. Le MansPont. PhoenixPont. SunbirdRenault Le CarS
    > ubaruToyota CelicaToyota CorollaToyota CoronaVW DasherVW DieselVW RabbitVW SciroccoVolvo 260
    
    .

  • #2
    Code:
    levelsof make, local(make_local) clean
    display "`make_local'"

    Comment


    • #3
      Thank you alejoforero. I should remember that circumstance of `"compound double-quoted string"'. Sorry for my stupid question.

      Comment


      • #4
        The issue in #1 is because the local macro was not wrapped in appropriate compound quotes:

        Code:
        display `"`make_local'"'
        What happened was that Stata expended the local macro correctly, but then display interpreted the result as a bunch of text to be displayed as one concatenated string, hence the loss of spaces separating makes.

        A safer approach to looking at the contents of local macros is to use -macro list-, noting that you need to append an underscore before local macro names as that is how Stata differentiates locals from globals.

        Code:
        macro list _make_local
        This will show exactly the contents of this macro .

        Comment


        • #5
          Thank you Leonardo Guizzetti for your detailed explanation. Somebody wrote a piece of code using
          display `localname'
          I find the results too hard to read, and I forget to try to use `""'
          Last edited by Chen Samulsion; 09 Nov 2021, 08:20.

          Comment

          Working...
          X