Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: getting the nth column name


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: getting the nth column name
Date   Tue, 6 Nov 2012 21:38:49 +0000

These are good solutions once the data are read in.

If the problem is to get at the third variable name before the dataset
is read in, something like

d using auto, varlist
use `: word 3 of `r(varlist)'' using auto

is needed.

In addition, -unab- is a neglected command here. See its documentation and/or

Cox, N.J. 2010. Stata tip 91: Putting unabbreviated varlists into
local macros. Stata Journal 10: 503-504.

A notable small difference between Joerg's solution and David's is
that Joerg used `= ' rather ` '. It won't bite for the 3rd variable
name, but the equals sign will truncate the string it evaluates, for
reasons explained in

SJ-8-4  pr0045  . . . . . . . . Stata tip 70: Beware the evaluating equal sign
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/08   SJ 8(4):586--587                                 (no commands)
        tip explaining the pitfall of losing content in a macro
        because of limits on the length of string expressions

Nick

Joerg Luedicke

You can use -ds- (-help ds-) to create a variable list and put the
name of the third variable in a local macro (-help extended_fcn-) for
further usage:

*----------------------
clear
sysuse auto
qui ds
local third : word 3 of `=r(varlist)'
sum `third'
*----------------------

David Radwin

> You can put the variable name in a local macro, like this:
>
> . ds
> . local var3 : word 3 of `r(varlist)'
> . describe `var3'

Pradipto Banerjee

>> I want to write a code to read the 3rd column from a few different
>> datasets. My issue is that the 3rd column can have different names in
>> different datasets. Is there any function or command that returns the
> name
>> of the Nth column ?
>>
>> For instance, suppose file F1 has the 3rd column as F1xyz and F2 has the
>> 3rd column as F2abc, I want a generic way to refer to F1xyz and F2abc
> with
>> an -ado- program?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index