Solved

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

Posted on 2012-03-20
7
908 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Assisted Solution

by:Aloysius Low
Aloysius Low earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now