Solved

How to define SAS datatset (with proper directories) in Unix Bash shell script?

Posted on 2012-03-20
7
930 Views
Last Modified: 2012-03-22
I am trying to define a SAS dataset directly from Unix bash shell script. The way that I am doing it in my Bash shell script is not working. I used to run SAS programs on VAX DCL code, so I am not very familiar with Unix Bash shell script. Please let me know what I am doing wrong?  

Note: SAS dataset "dataset1.sas7bdat" does exist in the "testing" directory in Unix.

Unix Bash shell script:
#script - tmp1.sh
#!/bin/bash
mydir=/2011/march/tuesday/testing
mydata=/2011/march/tuesday/testing/dataset2.sas7bdat
export mydir
export mydata
sas=/products/SAS/SASFoundation/9.2/sas
$sas test1
exit

Open in new window


SAS program:
#filename test1.sas
libname lib "$mydir";
data "$mydata";
   set lib.dataset1;
   if _N_ < 10 then output;
run;

Open in new window


I already tested my way and it's not working, so I am looking for some kind of another way of directly referencing a SAS dataset in the Unix Bash shell script.
Thank you!
0
Comment
Question by:labradorchik
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 37747263
can't be done AFAIK

is there any constraint that you have that you must do it this way? could you have the information stored in another SAS dataset/code and execute that file in the SAS program which will instantiate variables?
0
 

Author Comment

by:labradorchik
ID: 37747513

Thank you, lowaloysius!

I don't have to use this way, I can use any way that works, :) - I am just trying to understand what can be done and what can't be done in Unix bash shell script vs VAX DCL code (.COM code vs bash shell script).  Can I reference a SAS dataset within/directly from the Unix bash shell script or I have to/must run a SAS program with that SAS dataset through Bash script?

In DCL I usually used to reference/used logicals or just hardcoded directories with filenames like .sas7bdat, .LOG, .LIS, .SAS, or .DAT and that was very helpful. How I understand Unix bash shell script doesn't use logicals the same way as VAX DCL code. So far I figured out how I can use Unix logicals for referencing directories, but I am not sure how can I use Unix logicals for directories together with filenames?

Please also see another question that I have at this link:  
http://www.experts-exchange.com/OS/Unix/Q_27640685.html

Thank you!
0
 

Author Comment

by:labradorchik
ID: 37748134
lowaloysius,
in your question "could you have the information stored in another SAS dataset/code and execute that file in the SAS program which will instantiate variables?" by variables do you mean SAS dataset variables or logicals (with full directory path and file names like .LIS, .DAT, or .sas7bdat) that can be used in SAS program?

Before, I used the same logicals for both (VAX DCL code and SAS program) when referencing those logicals to full directory path and files like .LIS, .DAT, or .sas7bdat.  Now, since how I understand I can't use logicals for directories with files in Unix bash shell script, to me it appears that I can not use the same method in Unix bash shell script as I was using in VAX DCL code.
Please correct me if I am wrong or please explain how may I reference directories and file names like .LIS, .DAT, or .sas7bdat  in the best way in Unix bash shell script?

Note: I need this in the Unix bash shell script mainly for opening a file, writing to a file, or simply just deleting a file after Unix bash shell script finish running my .SAS programs.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Assisted Solution

by:Aloysius Low
Aloysius Low earned 500 total points
ID: 37751386
you'll need to do the initialization within a SAS code, either in the main, or in another SAS code which gets called by the main SAS code.

i haven't done VAX DCL before, so i'm not able to comment much on what is possible or not.

in fact, based on your description, it appears that you would like to have the initialization done once only, and to use it in both the SAS code as well as the UNIX batch command, which is not possible.

however, if your requirement is to open a file, writing to a file or deleting the file afte rthe UNIX bash shell executes the .SAS and you would like the initialization to be done only once, i suggest using the X command within the SAS code.

this X command can execute any UNIX/DOS command as if you are executing it from the command line.

if going by this, all your UNIX bash script needs to do is to execute the SAS code.

e.g.

#SAS CODE#
/*variable initialization*/
%let directory=/temp/;
%let file=mydata;

/*do something*/
libname temp "&directory";
data temp."&mydata";
  x=1;
run;

/*cleaning up*/
x "rm &directory.&file..sas7bdat";


#shell script#
sas -sysin [your sas code]

not sure if this is what you wanted...
0
 

Author Comment

by:labradorchik
ID: 37752878
lowaloysius, thank you very much for your comments!
Your code for SAS programs will definitely work for the "delete" purposes, but this is not the only function I am looking to implement in the Unix bash shell script after running my SAS programs.  I also need to open or append and write into files like .LOG, .LIS, .DAT, or .sas7bdat in Unix bach shell script.  
From your knowledge in Unix, do you know if some of these files like .LOG, .LIS, .DAT, or .sas7bdat can't be opened or appended in the Unix bash shell script?
May I do the same functions in the Unix bash shell script as I did in Vax DCL?
For example: I usually open .LOG files to automatically find errors in my .SAS programs, then open .LIS file to write some messages about a particular error, which resulted from the .SAS program after the run. Also, if I run a few .SAS programs within the DCL code then I append all .LOGs with a message for every .SAS program like "Module1 Successfully Processed" into one final .LIS file. All of these functions were done directly from the DCL code, so I think there should be a way to do exactly the same in the Unix bash shell script.    

Any of your comments will be greatly appreciated!
0
 
LVL 14

Accepted Solution

by:
Aloysius Low earned 500 total points
ID: 37752976
in unix, you can append text files together without any issues, or to write a value out to a file e.g.

to append fileA.txt and fileB.txt into fileC.txt, where fileC is a new file:
more fileA.txt > fileC.txt
more fileB,txt >> fileC.txt

if fileC is an existing file and you want to continue to append fileA and fileB into fileC, just change the first line to use >> instead of >
(>> appends into the file, creates one if not present, > creates a new file and writes in, overwriting the existing file if present)

similar concept can be used to write a message into a text file:
echo "hello" > fileC.txt

sas datasets (sas7bdat) would be tricky, as it will require a SAS code to do the appending

as for opening a log file to check for a certain message, you can actually have a UNIX shell script which contains the necessary logic... i don't have a copy, but i've seen it somewhere before on my project implementation sites... you can use a combination of UNIX shell commands to scan for a particular text and based on the result, write a message into a text file. the input to this 'utility' script would be the log file name, which can be executed by SAS using the X command as usual... if you need the exact code for the script, I guess you'll need to open another question for that, specifically in the UNIX thread :)

hope i have managed to addressed most of your questions and/or doubts

p.s. i have not touched UNIX for quite some time, so the commands above might not work correctly, although the idea/concept is there
0
 

Author Comment

by:labradorchik
ID: 37753060
This is exactly what I needed!!!!  Great examples too! I will open a few other different Unix/SAS questions later on today...
Thank you VERY MUCH!! :)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 75
jboss 7.1 start up error 1 64
IntelliJ and Eclipse Neon 2 installations 4 41
SQL Server Express automatically execute SQL or SP 8 37
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question