Statalist


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

st: RE: AW: creating an ordered month variable over several years


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: AW: creating an ordered month variable over several years
Date   Thu, 26 Nov 2009 11:00:38 -0000

Note that the alternative approach of assigning a date format to a
monthly date variable doesn't seem to help enough. 

gen mydate = ym(year, month)
format mydate %tmm_CY

followed by a graph with -by(mydate)- just uses the numbers and not the
format. Mapping that to a string causes display in alphabetical order
(all Aprils first) which is unlikely to be helpful. 

-catplot- is a user-written command from SSC, but is not causing this
behaviour. -graph bar, by()- is responsible. 

Perhaps there is a work-around. 

Nick 
[email protected] 

Martin Weiss

*************
clear*
set obs 100
gen id=_n		// subject id
gen month=1 + int(12*uniform())
gen year=2008 + int(2*uniform())
gen subject_type=1 + int(3*uniform())
la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
la val subject_type lab_subjects
list, noo

compress

//from http://www.stata-journal.com/sjpdf.html?articlenum=pr0013
//follow special sequences

//generate label for months
tokenize `c(Months)'

forvalues i = 1/12 {
 label def month `i' "``i''" , modify
 }

label val month month

decode month, generate(mon)
gen yearmonth=  mon + " " + string(year)

catplot bar subject_type, by(yearmonth) stack asyvars ///
		bar(1, bcolor(red)) 	///
		bar(2, bcolor(yellow))  ///
		bar(3, bcolor(green))
*************

Michael McCulloch

I'd appreciate any feedback on this question: I'd like to create a  
variable m_y, which I can sort in order of month over several years.

I've simulated data with:
. clear
. set obs 100
. gen id=_n		// subject id
. gen month=1 + int(12*uniform())
. gen year=2008 + int(2*uniform())
. gen subject_type=1 + int(3*uniform())
. la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
. la val subject_type lab_subjects
. list, noo

But, I'd like guidance on how to combine month and year into one  
ordered variable, to appear with labels such as:
	Jan 2008
	Feb 2008
	...
	Jan 2009
	Feb 2009

That ordered variable would then be displayed in the -by(month)-  
option in this graph:
	. catplot bar subject_type, by(month) stack asyvars ///
		bar(1, bcolor(red)) 	///
		bar(2, bcolor(yellow))  ///
		bar(3, bcolor(green))

I could use both -over- and -by-, but that looks awkward:
	. catplot bar subject_type, by(year) over(month) stack asyvars
///
		bar(1, bcolor(red)) 	///
		bar(2, bcolor(yellow))  ///
		bar(3, bcolor(green))


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index