I have a file test.txt.gpg, a text file encrypted with a pass phrase. How should I readin in SAS? I have gone through other posts and found that there is a way to decrypt first using X command and then readin. Need more inputs
if you use
--decrypt file.gpg
instead of
--decrypt-files file.gpg
the result will be written to stdout, so you can capture it with the filename pipe.
(retrieved from the first hit of a google search for "man gpg"; Maxim 1 is also valid for UNIX commands)
My syntax looks like
%MACRO DATA_READIN(LIB,FILE,DSN);
* Supplying the data to SAS through PIPE, as the files are compressed ;
FILENAME READER PIPE "M:\Rawdata\&FILE..txt.gpg";
DATA &LIB..&DSN. (COMPRESS=YES) ;
%LET _EFIERR_ = 0; /* set the ERROR detection macro variable */
INFILE READER DELIMITER = '|' TRUNCOVER DSD LRECL=32767 FIRSTOBS=1;
INPUT
I am able to decrypt the file through this but the program is end. The next lines of program aren't running.
Log says
NOTE: The infile READER is:
Unnamed Pipe Access Device,
PROCESS=M:\Rawdata\Test1.txt.gpg,
RECFM=V,LRECL=32767
NOTE: 0 records were read from the infile READER.
NOTE: The data set RSASDATA.T1 has 0 observations and 69 variables.
NOTE: DATA statement used (Total process time):
real time 6.36 seconds
cpu time 0.04 seconds
@Konakanchi wrote:
My syntax looks like
%MACRO DATA_READIN(LIB,FILE,DSN);
* Supplying the data to SAS through PIPE, as the files are compressed ;
FILENAME READER PIPE "M:\Rawdata\&FILE..txt.gpg";
DATA &LIB..&DSN. (COMPRESS=YES) ;
%LET _EFIERR_ = 0; /* set the ERROR detection macro variable */
INFILE READER DELIMITER = '|' TRUNCOVER DSD LRECL=32767 FIRSTOBS=1;
INPUT
I am able to decrypt the file through this but the program is end. The next lines of program aren't running.
Log says
NOTE: The infile READER is:
Unnamed Pipe Access Device,
PROCESS=M:\Rawdata\Test1.txt.gpg,
RECFM=V,LRECL=32767NOTE: 0 records were read from the infile READER.
NOTE: The data set RSASDATA.T1 has 0 observations and 69 variables.
NOTE: DATA statement used (Total process time):
real time 6.36 seconds
cpu time 0.04 seconds
That happens because M:\Rawdata\&FILE..txt.gpg is just a file and not something that can be run on the system.
In filename pipe, you need to supply a commandline that decrypts the file and writes the decrypted contents to standard output (which is what you then read in SAS).
So you need to first find out (see the gnupg documentation referenced earlier by @SimonDawson) how to do that.
Yep.
I have changed it to
FILENAME READER PIPE "gpg --decrypt-files &PATH.\Rawdata\&FILE..txt.gpg";
DATA &LIB..&DSN. (COMPRESS=YES) ;
%LET _EFIERR_ = 0; /* set the ERROR detection macro variable */
INFILE READER DELIMITER = '|' TRUNCOVER DSD LRECL=32767 FIRSTOBS=1;
I see that the file is decrypted in my folder but no SAS is not reading in.
Log says
Stderr output:
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
NOTE: 0 records were read from the infile READER.
NOTE: The data set RSASDATA.T3 has 0 observations and 69 variables.
if you use
--decrypt file.gpg
instead of
--decrypt-files file.gpg
the result will be written to stdout, so you can capture it with the filename pipe.
(retrieved from the first hit of a google search for "man gpg"; Maxim 1 is also valid for UNIX commands)
Worked. Thanks. However, I have another doubt. My gpg file as a gzip file , within which there is a text file. The text file needs to be imported into SAS. My code which has worked based on your suggestions to read a pgp encrypted text file was
FILENAME READER PIPE "gpg --decrypt &PATH.\Rawdata\Test3.txt.gpg";
DATA &LIB..&DSN. (COMPRESS=YES) ;
%LET _EFIERR_ = 0; /* set the ERROR detection macro variable */
INFILE READER DELIMITER = '|' TRUNCOVER DSD LRECL=32767 FIRSTOBS=1 OBS=10;
Where can i add the command to extract text file from gzip here?
So you have a file that was probably created with something like
gzip -c file.txt|gpg>file.txt.gpg
(I've omitted the gpg options for accepting stdin and writing to stdout)
So it is text compressed with gzip encrypted with gpg.
To read that, you need to unravel everything in the reverse order of its creation:
filename reader pipe "gpg --decrypt &PATH.\Rawdata\Test3.txt.gpg|gzip -dc";
So the decrypted stream is fed to gzip, which is told by the parameters to decompress and write to standard output; since no filename is given, gzip will read from stdin and therefore the pipe from gpg.
An alternative would be to decrypt to a file first, and then read that with filename zip with the gzip option.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.