*! version 1.0.1 November 2015
help nrow

Title

nrow -- Rename variables as their nth-row values.

Syntax

nrow [row#] [, keep varlist(varlist)]

Description

nrow is a simple command for easily renaming variables as their nth-row values.
When run it renames all variables as their first row values, as the firstrow option in import excel.
The optional argument row# can be specified to choose any row as the one to rename the variables for.
It must be any positive integer not greater than _N.
If the value of the nth-row of a particular variable is not a valid Stata name, the command automatically transforms it by use of the strtoname() function.
If varlist is specified, then the command only renames those variables.
The command defaults to droping all rows up to and including row#, but these rows can be kept using the keep option.

Examples

This example uses the nrow_example.dta ancillary dataset (included). It can be loaded with:
. sysuse nrow_example, clear

To set the first row as variable names we can simply type:
. nrow

This deletes the first row, though. If we want to keep that row, we need to reload the dataset and use the keep option:
. sysuse nrow_example, clear
. nrow, keep

Notice that the last two variables have a few illegal Stata variable characters which are replaced by underscores.
Since those names are intelligible, it may be better to use the second row as variable names:
. nrow 2, keep

Lastly, it may be preferable to use first row values for the first two variables, but then use second row values for the last three variables:
. nrow, varlist(id birth)
. nrow, varlist(var1 - var3)

Author

Alvaro Carril, J-PAL LAC, Chile

acarril@fen.uchile.cl