DATA Step, Macro, Functions and more

In reading binary file why is SAS skipping some characters?

Reply
PROC Star
Posts: 7,363

In reading binary file why is SAS skipping some characters?

I'm attempting to read a set of jpg files.  They all start off with the same characters, but I've been unable to correctly read one file.

As a test I tried to read the characters in one at a time.  The one file that is giving me problems has the following representation in hex:


FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 60 00 60 00 00 FF E1 5C 1C


the other files, for which I don't have problems, start off with:


FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 60 00 60 00 00 FF E1 5C 22


In this case of the first file, reading in 1 character at a time, it only reads FF D8 FF, then skips directly to E1 (i.e., the 22nd character).


Anyone have a clue?

Super User
Super User
Posts: 6,502

In reading binary file why is SAS skipping some characters?

Art - Can you post your code?  I did not have trouble reading those characters.

data _null_;

  do filename='file1.dat','file2.dat';

    file dummy filevar=filename recfm=f lrecl=1;

      do i=1 to 24 ;

        input char $hex2. +1 @;

        put char $char1. ;

      end;

      input;

  end;

  stop;

cards;

FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 60 00 60 00 00 FF E1 5C 1C

FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 60 00 60 00 00 FF E1 5C 22

run;

data _null_;

  infile 'file1.dat';

  input; put _infile_ $hex48.;

  infile 'file2.dat';

  input; put _infile_ $hex48.;

run;

FFD8FFE000104A46494600010101006000600000FFE15C1C

FFD8FFE000104A46494600010101006000600000FFE15C22

NOTE: 1 record was read from the infile 'file1.dat'.

      The minimum record length was 24.

      The maximum record length was 24.

NOTE: 1 record was read from the infile 'file2.dat'.

      The minimum record length was 24.

      The maximum record length was 24.

PROC Star
Posts: 7,363

In reading binary file why is SAS skipping some characters?

Tom,

The following is my test code:

%let path=c:\art\;

filename indata pipe "dir &path.*.jpg /b";

data test (keep=val counter foundit fname);

  length fil2read fname $80;

  infile indata truncover;

  informat f2r $50.;

  input f2r;

  fil2read="&path."||f2r;

  done=0;

  foundit=0;

  counter=0;

  infile dummy filevar=fil2read RECFM=f lrecl=1;

  fname=fil2read;

  do while(not done);

    input VAR1 $char1.;

    counter+1;

    val=input(var1,pibr2.);

    if var1 eq 'E' then foundit=1;

    output;

    if counter eq 30 then done=1;

  end;

run;

PROC Star
Posts: 7,363

In reading binary file why is SAS skipping some characters?

Tom (and anyone else who might be interested),

Upon running your code on my actual files I ended up seeing the same incorrect data.  As it turned out, the files I had been trying to analyze had been corrupted by MS.  I.e., in adding a subject line to each jpg, using MS's windows explorer, the files no longer followed the exif rules on which my analysis was based.

Art

Super User
Posts: 9,682

In reading binary file why is SAS skipping some characters?

Art. Maybe you can try to use another informat $phex.

filename jpg 'c:\logo.jpg';

data temp;

infile jpg ;

input x  $phex. @@;

if _n_ =100 then stop;

run;

Ksharp

Ask a Question
Discussion stats
  • 4 replies
  • 172 views
  • 3 likes
  • 3 in conversation