Statalist


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

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


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: creating an ordered month variable over several years
Date   Thu, 26 Nov 2009 09:32:42 +0100

<> 



*************
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))
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Michael
McCulloch
Gesendet: Donnerstag, 26. November 2009 03:03
An: Statalist Statalist
Betreff: st: creating an ordered month variable over several years

Hello Statalist friends,
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))



Best wishes,

Michael McCulloch
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391


*
*   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/


*
*   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