DATA Step, Macro, Functions and more

Writing text strings & text files - using Line Feeds causes additional lines or trailing spaces.

Reply
Contributor jp
Contributor
Posts: 29

Writing text strings & text files - using Line Feeds causes additional lines or trailing spaces.

I'm trying to create an AWS signature, however I can't get my Line Feeds ('0a'x) to work exactly right when I'm trying to output a textfile of the string I want to hash. I'm using a textfile to check that the string (to be stored as a macro variable) is correct and can be hashed correctly.

 

Due to the line feeds not being exact it's causing the hash to be different from what I'm expecting (even when hashing the string directly)

 

Here's what I have currently; from googling around various documents:

 


data _null_;
  file "W:\sigtest.txt" TERMSTR=lf   recfm=v;
  signature = 'AWS4-HMAC-SHA256' || '0a'x ||
              '20150915T124500Z' || '0a'x ||
              '20150915/us-east-1/s3/aws4_request' || '0a'x || 
              'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';

  put signature;
run;

However when I check the output in notepad++ I get the following:

sig sas.PNG

 

When I'm expecting the following:

sig std.PNG

 

The extra line causes the hash to be different, however if I drop:

TERMSTR=lf   recfm=v

and use:

recfm=f

 I get the requisite number of lines (4), but I have trailing blanks/spaces after the hash key, which I can't get rid of.

sig sas2.PNG

 

Appreciate any help anyone has for this problem.

Respected Advisor
Posts: 3,777

Re: Writing text strings & text files - using Line Feeds causes additional lines or trailing spa

You need RECFM=N if you want to write your own term-string.  You also need a format you are getting a extra space using LIST PUT.

 

data _null_;
   file "~/sigtest.txt" recfm=n;
   signature = 'AWS4-HMAC-SHA256' || '0a'x ||
              '20150915T124500Z' || '0a'x ||
              '20150915/us-east-1/s3/aws4_request' || '0a'x || 
              'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';
   l=length(signature);
   put signature $varying256. l;
   run;
data _null_;
   infile "~/sigtest.txt" recfm=f;
   input;
   list;
   run;   

Capture.PNG

Contributor jp
Contributor
Posts: 29

Re: Writing text strings & text files - using Line Feeds causes additional lines or trailing spa

My colleague suggested the following solution:

 

data _null_;
  file "W:\sigtest.txt" TERMSTR=lf   recfm=F lrecl=1;
  signature = 'AWS4-HMAC-SHA256' || '0a'x ||
              '20150915T124500Z' || '0a'x ||
              '20150915/us-east-1/s3/aws4_request' || '0a'x ||
              'ef7c45cc2b0f100ea5d65024643f5cbaf83e7ba2717108905acd605cfe17bc6b';

  put signature;
run;

I will admit I'm not 100% sure how/why this is working but it seems to work. I'll take a look at your solution too data _null_.

 

Respected Advisor
Posts: 3,777

Re: Writing text strings & text files - using Line Feeds causes additional lines or trailing spa

RECFM N and F are similar in that there is no record termination string, RECFM=N has no record structure while RECFM=F has logical structure (an external attribute).  Standing on the outside looking in both files look them same because they are (tested with unix diff)

 

Your example writes 133 one byte records.  You can drop the INFILE statement option TERMSTR it is ignored.

Ask a Question
Discussion stats
  • 3 replies
  • 216 views
  • 0 likes
  • 2 in conversation