SAS: Unzipping multiple .dat files at once - zip

I want to unzip a file that contains a .dat file using SAS. I have over 100 files to unzip and therefore I want to do it automatically with SAS. I've tried to use the following:
FILENAME ZIPFILE SASZIPAM 'Z:\folder\file';
DATA newdata;
INFILE ZIPFILE(file.dat) dsd DLM='|';
INPUT var1 var2 var$;
RUN;
That doesn't work. Is there a problem when you use ZIPFILE SASCIPAM to unzip a .dat file? I have SAS 9.3.
Is there a better alternative?

I guess you could use macros to do it, but it might get a bit messy. SAS has for many years offered the X command for contacting the command prompt. This method requires that you have downloaded a free file archiver/de-compresser (e.g. 7-Zip, WinRAR etc.).
I like using the command line version of 7-Zip. I use a 32-bit machine, so you might have to use a different .exe file.
The syntax for unzipping multiple files in a directory:
data _null_;
X "cd (7-Zip_installed_location)"; <=== File that contains the 7za.exe
X "7za e (zip_files_location)*.zip -o(output_destination)";
run;
For Example, I have some zip files in the folder called "Compressed":
data _null_;
X "cd C:\7-Zip Comm";
X "7za e C:\sasdata\Compressed\*.zip -oC:\sasdata\New";
run;
e stands for "extract".
* means for all.
-o stands for "output".

I have never seen SASZIPAM as a file reference type.
I would do this like this:
filename zipfile ZIP 'Z:\folder\file.zip';
DATA newdata;
INFILE ZIPFILE(file.dat) dsd DLM='|';
INPUT var1 var2 var$;
RUN;
It's possible I am missing that type in the documentation. Can you paste the ERROR you get in your log?

Double quotes for each of the path will resovle any issue while extracting .z file on windows 7 (SAS 9.3): (FYI - 2nd statement is in one line)
data _null_;
x '"C:\Program Files\7-Zip\7z.exe" x "E:\sas\config\Lev1\SASApp\Data\*.Z" -o"E:\sas\config\Lev1\SASApp\Data"';
run;
Regards
Bhavin

Related

Using Python to copy contents of multiple files and paste in a main file

I'll start by mentioning that I've no knowledge in Python but read online that it could help me with my situation.
I'd like to do a few things using (I believe?) a Python script.
I have a bunch of .yml files that I want to transfer the contents into one main .yml file (let's call it Main.yml). However, I'd also like to be able to take the name of each individual .yml and add it before it's content into Main.yml as "##Name". If possible, the script would look like each file in a directory, instead of having to list every .yml file I want it to look for (my directory in question only contains .yml files). Not sure if I need to specify, but just in case: I want to append the contents of all files into Main.yml & keep the indentation (spacing). P.S. I'm on Windows
Example of what I want:
File: Apes.yml
Contents:
Documentation:
"Apes":
year: 2009
img: 'link'
After running the script, my Main.yml would like like:
##Apes.yml
Documentation:
"Apes":
year: 2009
img: 'link'
I'm just starting out in Python too so this was a great opportunity to see if my newly learned skills work!
I think you want to use the os.walk function to go through all of the files and folders in the directory.
This code should work - it assumes your files are stored in a folder called "Folder" which is a subfolder of where your Python script is stored
# This ensures that you have the correct library available
import os
# Open a new file to write to
output_file = open('output.txt','w+')
# This starts the 'walk' through the directory
for folder , sub_folders , files in os.walk("Folder"):
# For each file...
for f in files:
# create the current path using the folder variable plus the file variable
current_path = folder+"\\"+f
# write the filename & path to the current open file
output_file.write(current_path)
# Open the file to read the contents
current_file = open(current_path, 'r')
# read each line one at a time and then write them to your file
for line in current_file:
output_file.write(line)
# close the file
current_file.close()
#close your output file
output_file.close()

Extract tar.gz{some integer} in python

I am trying to extract a file name with this format--> filename.tar.gz10
I have tried mutpile wayd but for all of them, I get the error that is unknow format. it works fine for files ends with tar.gz00. I tried to change the name but still does not work.
Here are what I have tried,
import tarfile
file = tarfile.open('filename.tar.gz10')
file.extractall('./extracted_path')
file.close()
Another way is,
shutil.unpack_archive('./filename.tar.gz10', './extracted_path', 'tar.gz17')
Thanks for your help in advance.
This coule be because the archive was split into smaller chunks, on linux you could do so using the split -b command so one big file is actually multiple smaller ones now, and they are named like
file.tar.gz01
file.tar.gz02
file.tar.gz03
file.tar.gz04
etc...
you wont be able to decompress these file individually, so you have to concatenate them first into one file then decompress.
To verify whther it was split or not, run file {filename} and if does not recognize it as a gzip compressed archive then it is propably split (this is why you get unknown format error)
You can try to do the following:
from glob import glob
import os
path = '/path/to/' # location of your files
list_of_files = glob(path + '*.tar.gz*') # list all gzip files
bash_command = 'gzip -dk filename.tar.gz' + ' '.join(list_of_files) # create bash command to concatenate the files
os.system(bash_command)

Adapt command to creating csv file from storage content including date(time) & file size also

According to thread:
Linux: fast creating of formatted output file (csv) from find command
there is a suggested bash command, including awk (which I don't understand):
find /mnt/sda2/ | awk 'BEGIN{FS=OFS="/"}!/.cache/ {$2=$3=""; new=sprintf("%s",$0);gsub(/^\/\/\//,"",new); printf "05;%s;/%s\n",$NF,new }' > $p1"Seagate-4TB-S2-BTRFS-1TB-Dateien-Verzeichnisse.csv"
With this command, I am able to create a csv file containing "05;file name;full path and file name" of the directory and file content of my device mounted on /mnt/sda2. Thanks again to -> tink
How must I adapt the above command to receive date(&time) and file size also?
Thank you in advance,
-Linuxfluesterer

BASH: How to copy the name of the file and insert it into the text using script?

So I need to create a lot of text files.
Each of the text file is named AAAAA.txt, BBBBB.txt, CCCCC.txt and etc etc.
Within each text file, all the content is as follows:
1.Copy "to-be-replaced".txt into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
So, I need to write a script, that copies the name of the file (AAAAA, BBBBB, and so on) and then place it in the "to-be-replaced" within its content.
How can I do that? need some idea please.
Thank you~~
MT32
Use a HERE document which expands variables if the delimiter isn't quoted:
#!/bin/bash
for char in {A..Z} ; do
filename=$char$char$char$char$char.txt
cat <<EOF > $filename
1.Copy $filename into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
EOF
done

Octave. dlmread: unable to open file 'myfile.txt'

I have a txt file with data in form: 2104,3,399900 i_e int1,int2,int3 I have 50 rows in the same format.
Now I can want to put the data into variable, say, a.
I am using the the command :
a = csvread('ex1data2.txt');
%I have tried a = dlmread('ex1data2.txt'); it too does't work
it produces an error :
error: dlmread: unable to open file 'ex1data2.txt'.
I have added the path of the directory that have file to the octave search paths.
How can I read the text file?
Thanks.

Resources