Thursday, June 27, 2013

Unix: System date and time to a variable, and using that variable

Caveat: I am a Unix novice.  There may be better ways to do this, but this worked for me.

Show system date and time in the default format:
$ date
Thu Jun 27 21:23:36 CDT 2013

Show system date and time in yymmdd_hhmmss format (two digit year):
$ date "+%y%m%d_%H%M%S"

Show system date and time in yyyymmdd_hhmmss format (four digit year):
$ date "+%Y%m%d_%H%M%S"

Save the system date and time in yyyymmdd_hhmmss format to a variable, then show it:
$ var="$(date '+%Y%m%d_%H%M%S')"
$ echo datetime is $var
datetime is 20130627_212437

My requirements: Schedule a SAS program ( to run at 4am on June 28th.  Send the SAS log to a file whose name includes the system date and time so we can archive the logs.

Step 1: Create a (script) file called go
ts="$(date '+%Y%m%d_%H%M%S')"
echo "sas -log pd_$ts.log" | at 0400 Jun 28
Step 2: Execute a (script) file called go
$ . go

Monday, June 24, 2013

Python: Reading a JSON dataset

I just completed an online Python course.  I have been experimenting and there is much about this language to love!

Here is a simple JSON dataset, called min_max_json.txt:

"min": 34,
"max": 82

Here is the source code, called, which reads the JSON dataset and calculates the range (defined as max - min):

import json
data = json.loads(open('min_max_json.txt').read())
print("This is what was read in:")
min = data["min"]
max = data["max"]
range = max - min
print("The range is " + str(range) + ".")

Here is the output:

This is what was read in:
{'min': 34, 'max': 82}
The range is 48.

Note: Using Python 3.3.2 with IDLE.