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

Posted on 2012-03-20
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 -
export mydir
export mydata
$sas test1

Open in new window

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

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!
Question by:labradorchik
  • 4
  • 3
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?

Author Comment

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:

Thank you!

Author Comment

ID: 37748134
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.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.


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

/*do something*/
libname temp "&directory";
data temp."&mydata";

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

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

not sure if this is what you wanted...

Author Comment

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!
LVL 14

Accepted Solution

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

Author Comment

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!! :)

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

785 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