[Tutor] Re[2]: [Tutor] ksh and SAS questions

adeline_j_wilcox@ccMail.Census.GOV adeline_j_wilcox@ccMail.Census.GOV
Fri, 04 Jun 1999 16:28:15 -0400


     Well, I can't see the error in the following script. Running 
     SunOS 5.5.1. When I run my Python script, I get this error 
     message:
     
Traceback (innermost last):
  File "recheck.py", line 14, in ?
    month = os.environ['CATI']
  File "/usr/local/lib/python1.5/UserDict.py", line 12, in __getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: CATI

     The script is:
     
     1  #!/usr/local/bin/python
     2  #PROGRAM: recheck.py
     3  #FUNCTION: execute Barbara's RECHECK program
     4  #INPUTS: rofr.txt
     5  #OUTPUTS: rofr.sas, ccMail message
     6  #CALLS TO: Python os, sys, and string modules, recheck.sas
     7  #SPECIAL NOTES: Have not yet figured out how to automate input of the 
rofr file
     8  #AUTHOR: Adeline Wilcox         DATE: 05Apr99
     9  #Written with the assistance of Andrew Kuchling and especially Tim 
Peters
    10  #UPDATED:                       DATE: 
    11  import os, sys 
    12  os.system('/op/ctl/prod/scripts/setcm.ksh')
    13  #get CATI month from environment
    14  month = os.environ['CATI']
    15  import string
    16  sys.stdin = open('/op/ctl/prod/data/ro' + month +'.txt','r')
    17  lines = sys.stdin.readlines()
    18  for i in range(len(lines)):
    19      line = lines[i]
    20      lines[i] = "'" + string.rstrip(line) + "'"
    21  sys.stdout= open('/op/ctl/test/data/rofr.sas','w')
    22  print "if rofr in (" + string.join(lines, ", ") + ") then output;"
    23  #if I dont close the file, SAS does not get quotes around last value of 
rofr
    24  sys.stdout.close()
    25  #os.system("$SASDIR/sas /op/ctl/prod/prgms/recheck.sas -sysparm " + 
month + \ " -log /op/ctl/prod/logs/recheck.log." + month ")
    26  #os.system("mailx -s 'Recheck " + month + " run for you \' 
Adeline.J.Wilcox@ccmail.census.gov ")


Adeline



______________________________ Reply Separator _________________________________
Subject: Re: [Tutor] ksh and SAS questions 
Author:  Corran Webster <Corran.Webster@math.tamu.edu> at SMTP-GATEWAY
Date:    5/12/1999 5:52 PM


> Using a script named recheck.py, I call a ksh script named 
> setcm.ksh. That script exports some shell variables. I would like to
> substitute the value of one shell variable in particular, named CATI,
> for the value 199904 in lines 14, 23 and 24. Please note that
> I want to pass the value of this shell variable to a SAS program with the
> SYSPARM option in line 23.

The os.environ variable gives you access to the UNIX environment variables,
so:

import os
month = os.environ['CATI']

puts the value as a string in the variable month.

> Can anyone tell me how to get ksh, Python and SAS working together here?

I've appended an updated version of the program which should do what I think
you want.  I've also fixed up what I think was a major inefficiency by moving
the write to the file "rofr.sas" out of the for loop (if this isn't what you
want, then you can easily put it back into the loop).  I also prettified
it a bit.

> I have neither C programming nor OOP experience. Also very new to Python.
> Have done a little reading of Lutz 1996 Programming Python. Reference
> to the Python documentation on the Web could be helpful.

The library documentation on www.python.org whould tell you what you need
to know about the os module and the os.environ variable.  If you have
questions about what's going on in the script, please ask.

Corran

----
#!/usr/local/bin/python
#PROGRAM: recheck.py
#FUNCTION: execute Barbara's RECHECK program
#INPUTS: rofr.txt
#OUTPUTS: rofr.sas, ccMail message
#CALLS TO: Python os, sys, and string modules, recheck.sas
#AUTHOR: Adeline Wilcox         DATE: 05Apr99
#Written with the assistance of Andrew Kuchling and especially Tim Peters
#UPDATED:                       DATE: 

import os, sys 
os.system('/op/ctl/prod/scripts/setcm.ksh')

# get value of CATI from environment
month = os.environ['CATI']

import string

sys.stdin = open('/op/ctl/prod/data/ro' + month + '.txt','r')

lines = sys.stdin.readlines()
for i in range(len(lines)):
    line = lines[i]
    lines[i] = "'" + string.rstrip(line) + "'"

sys.stdout= open('/op/ctl/prod/data/rofr.sas','w')
print "if rofr in (" + string.join(lines, ", ") + ") then output;"
#if I dont close the file, SAS does not get quotes around last value of rofr
sys.stdout.close()

os.system("$SASDIR/sas /op/ctl/prod/prgms/recheck.sas -sysparm " + month + \
        " -log /op/ctl/prod/logs/recheck.log." + month)
os.system("mailx -s '" + month + " for you' " + \
        "Adeline.J.Wilcox@ccmail.census.gov < rc" + month + ".txt")