Record Unique Variable Values in a Word Document

The putdocx command was added in Stata v15 and has been expanded with new features in Stata v16. It allows you to create Microsoft Word documents from within Stata. The command allows you to embed Stata outputs or results as tables or paragraphs, embed Stata graphs as images, and create formatted paragraphs – all without ever having to open Microsoft Word.

There are some unique ways this can be used together with other commands in Stata. Here we look at how the command -levelsof- can be used with -putdocx- and some macros to record a list of distinct (unique) values (observations) in a variable.

How to Use:

There are two important pieces of syntax in the above list of commands that it is important you understand. The first is the “foreach” line above. This is running a loop and must be typed in and run exactly as shown above, meaning each line must be entered into the console before the next one is typed.

The second important piece of syntax is how macros are called in Stata. To call a macro you start by using a grave (or open quote) – which is located on the tilde (~) key on your keyboard, next to the number 1. You end a macro by using an apostrophe (or closing quote)– which is located on the quotes (“) key on your keyboard, next to the enter key. So on the line directly after the foreach line above – within the brackets () – the sequence is grave (`); quotes (“); grave (`);(the letter) i; apostrophe (‘); quotes (“); apostrophe (‘).

Worked Example 1:

In this example I am going to use the auto dataset to record a list of car makes. As the car makes are stored within the “make” variable, I save the list of distinct values (in this case strings) using the levelsof command. I then create a new document and append the list to it. In the command pane I type the following:

This produces a list of cars contained in the variable “make”, placing a comma and a space between each make. The following is what was saved in the Word file “example.docx”:

This is a list of car makes:

AMC Concord, AMC Pacer, AMC Spirit, Audi 5000, Audi Fox, BMW 320i, Buick Century, Buick Electra, 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 Accord, 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 Supr, Olds Cutlass, Olds Delta 88, Olds Omega, Olds Starfire, Olds Toronado, Peugeot 604, Plym. Arrow, Plym. Champ, Plym. Horizon, Plym. Sapporo, Plym. Volare, Pont. Catalina, Pont. Firebird, 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 Scirocco, Volvo 260,

Worked Example 2:

The output in worked example 1 gives a nice list of car makes, however the formatting is a little off. The list ends with a comma and a space, just like all the entries prior to the last one. A properly formatted list would have a full stop at the end with the word “and” between the last and second-last entries to enhance readability. You could easily open the word document and fix it manually, but lets look at how to do this using the putdocx command. In the command pane I type the following:

When you open the document you can see that the last line has changed to:

Subaru, Toyota Celica, Toyota Corolla, Toyota Corona, VW Dasher, VW Diesel, VW Rabbit, VW Scirocco, and Volvo 260.

24 views0 comments

Recent Posts

See All