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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: AW: st: AW: identify all variables with dates


From   Eric Booth <ebooth@ppri.tamu.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: AW: st: AW: identify all variables with dates
Date   Wed, 3 Mar 2010 16:38:03 -0600

>

As others have mentioned, this is another reason that you should be strategic about naming your variables.   I was curious about what could be done if some of the other string vars in your dataset had a type 10 or if you had one of these date variables where the days or months didn't have the leading zeroes (that is, some of the date variables were all dates like "1/1/2008" while others were "01/01/2008").  To extend Martin's example a bit, here's a clunky way to search for variables which can be formatted as dates, format them, and leave the other string variables alone.  There's likely a better/faster/more efficient way to do this, but here's one way I got it to work:

*****
clear*

inp str10 mydate byte othervar  /* 
*/ str5 otherstring str10 yetanotherdate
"13/12/2007"	01 "hdgs" "13/12/2007"
"14/12/2007"	02 "hdgs" "14/12/2007"
"17/12/2007"	03 "hdgs" "17/12/2007"
"18/12/2007"	04 "hdgs" "18/12/2007"
"19/12/2007"	05 "hdgs" "19/12/2007"
"20/12/2007"	06 "hdgs" "20/12/2007"
"21/12/2007"	07 "hdgs" "21/12/2007"
"27/12/2007"	08 "hdgs" "27/12/2007"
end

***make the "otherstring var 10 characters as well**
replace others = otherstring + "hhhhhh"


ds, has(type 10)
di "`r(varlist)'"

foreach x in `r(varlist)' {
	g `x'1 = date(`x', "DMY")
	cap drop tag
	mark tag
	markout tag `x'1,
	qui sum tag
	if `r(max)'==1 {
		format `x'1 %td
		drop `x' 
		rename `x'1 `x'
		drop tag
	}
	if `r(max)'==0  {
		drop `x'1
		drop tag
		}
	}

*****

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754





On Mar 3, 2010, at 3:46 AM, Martin Weiss wrote:

> 
> <> 
> 
> Everything rides on the question: How many other vars are there floating
> around that Stata might mistake for dates, and how do they differ from the
> ones you want? The heuristic could be as easy as searching for strings of
> length 10:
> 
> 
> *************
> clear*
> 
> inp str10 mydate byte othervar  /* 
> */ str5 otherstring str10 yetanotherdate
> "13/12/2007"	01 "hdgs" "13/12/2007"
> "14/12/2007"	02 "hdgs" "14/12/2007"
> "17/12/2007"	03 "hdgs" "17/12/2007"
> "18/12/2007"	04 "hdgs" "18/12/2007"
> "19/12/2007"	05 "hdgs" "19/12/2007"
> "20/12/2007"	06 "hdgs" "20/12/2007"
> "21/12/2007"	07 "hdgs" "21/12/2007"
> "27/12/2007"	08 "hdgs" "27/12/2007"
> "28/12/2007"	09 "hdgs" "28/12/2007"
> "02/01/2008"	10 "hdgs" "02/01/2008"
> "03/01/2008"	11 "hdgs" "03/01/2008"
> "04/01/2008"	12 "hdgs" "04/01/2008"
> "07/01/2008"	13 "hdgs" "07/01/2008"
> "08/01/2008"	14 "hdgs" "08/01/2008"
> "09/01/2008"	15 "hdgs" "09/01/2008"
> end
> 
> ds, has(type 10)
> *************
> 
> 
> 
> HTH
> Martin
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von raoul reulen
> Gesendet: Mittwoch, 3. März 2010 10:24
> An: statalist@hsphsun2.harvard.edu
> Betreff: Re: st: AW: identify all variables with dates
> 
> Hi Martin
> 
> Thanks for your suggestions. The problem is indeed like you describe.
> The potential date variables are stored as strings in the following
> format: dd/mm/yyyy. How would I identify such variables? Many thanks
> for your help, it is much appreciated.
> 
> Raoul
> *



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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