Monday, July 16, 2012

Quote of the moment

In a previous post, I mentioned the creation of a personal Stata .ado file, -quote-, that, when invoked, returns a randomly selected quote from a dataset of quotes I keep on my desktop.  The program, though, uses the current date as the 'seed' for generating the list of randomized numbers so a repeated calling of the -quote- command on the same day will return the same quote.  I wanted to change this so that during those episodes of non-productivity and distraction while working in Stata, a repeated calling of the -quote- command would return (more often than not) a different quote.  I also wanted to modify the .ado file so that a calling of the -quote- command would -preserve- any data in memory then -restore- it.  The former I accomplished by changing the 'seed' from the current day (number of days since 1960) to the current time (number of seconds since 01/01/1960 00:00:00) and the latter I fixed with the correct placement of -preserve- and -restore- in the .ado file.  The full content of the .ado file is pasted below.  Note that I retained the code for using the current date as the 'seed' but have commented it out (the line is preceded with an asterisk); the line following it establishes the current time as the 'seed'. 

*! version 1.0 \ cjt 2011-08-04
*! version 2.0 \ cjt 2012-07-12

// program:  quote.ado
// task:  -read- in dataset of quotes from desktop, randomize them, then display one
// project:  n/a
// author:    cjt
// born on date:  04 August 2011
// updated:  (20120712) changed the system parameter used for the seed from date to time
//        such that the command can be called repeatedly w/ a different quote appearing each
//        time.  also added -preserve- and -restore- so that -quote- can be used when another
//        dataset is open w/out clearing the data.


// #0
// program setup

capture program drop quote
program define quote
version 11.2
preserve            // preserve data in memory, if applicable
clear all
macro drop _all
set more off


// #1
// -read- in quote dataset
use "C:\Documents and Settings\cjt\Desktop\QuoteList.dta"


// #2
// assign observation number to quotes
gen obsno = _n
order obsno quote


// #3
// generate random numbers from the uniform distribution
* macro out the current date to an integer (# days since 01/01/1960)
*local cdate = date(subinstr("$S_DATE" , " " , "" , .), "DMY")
local ctime = clock(subinstr("$S_TIME" , " " , "" , .), "hms")
* set seed to current time...
*set seed `cdate'
set seed `ctime'
* random number from uniform distribution
gen xselect = runiform()
order obsno xselect


// #4
// -sort- by random number then print first quote
sort xselect
* macro out first quote
local quote1 = quote[1]

* display the quote
display "Quote of the moment:  " _newline _col(5) "`quote1'"

clear all

restore        // restore data in memory, if applicable.

end 


A sampling of consecutive calls to the -quote- program:

. quote
Quote of the moment: 
    After winning an argument with his wife, the wisest thing a man can do is apologize. (Anonymous)

. quote
Quote of the moment: 
    Denial ain't just a river in Egypt. (Mark Twain)

. quote
Quote of the moment: 
    You can't buy love, but you can pay heavily for it. (unattributed)

. quote
Quote of the moment: 
    We may have all come on different ships, but we're in the same boat now. (Martin Luther King, Jr.)

. quote
Quote of the moment: 
    Doubt is not an agreeable condition, but certainty is an absurd one. (Voltaire)

. quote
Quote of the moment: 
    Common sense is not so common. (Voltaire)

. quote
Quote of the moment: 
    To speak much is one thing, to speak well is another. (Sophocles)

. quote
Quote of the moment: 
    A bookstore is one of the only pieces of evidence we have that people are still thinking. (Jerry Seinfield)

. quote
Quote of the moment: 
    They've finally come up with the perfect office computer. If it makes a mistake, it blames another computer. (Milton Berle)

. quote
Quote of the moment: 
    Fashion is only the attempt to realize Art in living forms and social intercourse. (Oliver Wendell Holmes)

No comments:

Post a Comment