Solved

How to open SAS dataset in Unix bash shell script and extract a certain variable from that dataset?

Posted on 2012-03-22
5
1,080 Views
Last Modified: 2012-03-26
I am looking for an example of how to open .DAT in the Unix bash shell script and then extract a particular variable from that .DAT file into a different file.

Please note: this is how this function was done in VAX DCL code

open/read indat myfile.dat
 read_loop: 
  read/end_of_file=endit indat var1
  var2=f$extract(0,2,var1)
   if f$search("mydir1:file2.s''var2'") .nes. " " 
    then  
    del mydir1:file2.s'var2';* 
   endif
endit:
 close indat
 del myfile.dat;*

Open in new window


Any examples or comments on how this might be done in the Unix bash shell script will be greatly appreciated!!
0
Comment
Question by:labradorchik
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37755994
> .. extract a particular variable ..
could you please give an example what you want to extract

maybe this helps:
  grep  " mydir1" myfile.dat
0
 
LVL 39

Accepted Solution

by:
noci earned 500 total points
ID: 37760232
This would be roughly equivalent code, assuming you use subdirectories in stead of logicals.
Environment variables like var can be used to (see example 2)

cat  myfile.dat | while read var1           # read...
do
  var2=$(echo ${var1} | cut -b1-2 )          # f$extract
   if -f  mydir1/file2.s${var2}             # f$search
   then
       rm mydir1/file2.s${var2}
   fi
done
rm myfile.dat

#Example 2
MYDIR=/wherever/mydir1
cat  myfile.dat | while read var1           # read...
do
  var2=$(echo ${var1} | cut -b1-2 )          # f$extract
   if -f  ${MYDIR}/file2.s${var2}             # f$search
   then
       rm ${MYDIR}/file2.s${var2}
   fi
done
rm myfile.dat

#example3
for prefix in $( cut -b1-2 <myfile.dat)
do
   rm mydir1/file2.s${prefix}
done
0
 

Author Comment

by:labradorchik
ID: 37767366
noci, thank you very much for your help!!!
I probably will use your example #2. Looks very different from the VAX DCL code.
0
 
LVL 39

Expert Comment

by:noci
ID: 37768463
example1 & 2 stay relatevely close to the DCL original...
example 3 is most unix like...
And you're right bash shell doesn't look like DCL.
0
 

Author Comment

by:labradorchik
ID: 37768524
Thank you for your comments!!
0

Featured Post

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!

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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 …

760 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

21 Experts available now in Live!

Get 1:1 Help Now