DATA Step, Macro, Functions and more

How can I convert the attached txt file into the attached sas file?

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,636
Accepted Solution

How can I convert the attached txt file into the attached sas file?

Dear all,

I have hundreds of  similar txt files. What is the best way to convert them into one SAS dataset?

Thanks in advance!

Attachment
Attachment

Accepted Solutions
Solution
‎10-19-2012 06:22 PM
PROC Star
Posts: 7,364

Re: How can I convert the attached txt file into the attached sas file?

Linlin: The following worked for me:

%let path=c:\art\test\;

filename files pipe "dir  /b &path.*.txt";

data want;

  infile files truncover;

  informat id $20.;

  informat date ddmmyy10.;

  informat time time8.;

  format date ddmmyy10.;

  format time time8.;

  informat Participant_Group $15.;

  informat Participant_Name $23.;

  informat Block_Name $25.;

  informat Trial_Name $25.;

  informat Event_Name $25.;

  informat Participant_response $18.;

  informat key $1.;

  informat pressed_or_released $8.;

  informat correct_response $15.;

  informat error_code $4.;

  retain id date time;

  input fname $40.;

  fname=cats("&path.",fname);

  infile dummy filevar=fname dlm='09'x dsd truncover end=done;

  n=0;

  do while(not done);

    n+1;

    if n eq 1 then do;

      input /id/date time///;

    end;

    else do;

      input Participant_Group Participant_Name Block_Name Trial_Name

      Event_Name Participant_response key pressed_or_released

      correct_response error_code reaction_time;

      output;

   end;

  end;

run;

View solution in original post


All Replies
Solution
‎10-19-2012 06:22 PM
PROC Star
Posts: 7,364

Re: How can I convert the attached txt file into the attached sas file?

Linlin: The following worked for me:

%let path=c:\art\test\;

filename files pipe "dir  /b &path.*.txt";

data want;

  infile files truncover;

  informat id $20.;

  informat date ddmmyy10.;

  informat time time8.;

  format date ddmmyy10.;

  format time time8.;

  informat Participant_Group $15.;

  informat Participant_Name $23.;

  informat Block_Name $25.;

  informat Trial_Name $25.;

  informat Event_Name $25.;

  informat Participant_response $18.;

  informat key $1.;

  informat pressed_or_released $8.;

  informat correct_response $15.;

  informat error_code $4.;

  retain id date time;

  input fname $40.;

  fname=cats("&path.",fname);

  infile dummy filevar=fname dlm='09'x dsd truncover end=done;

  n=0;

  do while(not done);

    n+1;

    if n eq 1 then do;

      input /id/date time///;

    end;

    else do;

      input Participant_Group Participant_Name Block_Name Trial_Name

      Event_Name Participant_response key pressed_or_released

      correct_response error_code reaction_time;

      output;

   end;

  end;

run;

Super Contributor
Posts: 1,636

Re: How can I convert the attached txt file into the attached sas file?

Hi Art,

I knew you would help me.  Thank you very much!  The real txt file names cause problem. The real names are something like:

CC0001-V18-190810-PPI.txt.  Your code worked great after I renamed the text file. Below is the error message caused by the real names:

ERROR: Physical file does not exist, C:\temp\forum\folder2\CC0001-V18-190810-.

RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7---

1         CC0001-V18-190810-PPI.txt 25

PROC Star
Posts: 7,364

Re: How can I convert the attached txt file into the attached sas file?

Just expand the length of fname.  I didn't even explicitly assign a length, but used $40. to input it.  Assign a length of around $80 and see if that corrects the problem.

Super Contributor
Posts: 1,636

Re: How can I convert the attached txt file into the attached sas file?

Thank you Art!!!Smiley Happy  I changed the length to $80 and the problem is gone. - Linlin

Super Contributor
Posts: 1,636

Re: How can I convert the attached txt file into the attached sas file?

Another question:

I have two labtops. My old one has 9.3, the newer one has 9.2.  Art's code works on the one with sas 9.3.  not on the one with 9.2. Below is from the log file. Is the problem related to the PCs or SAS?

Thanks!

177  %let path=C:\temp\Mexico123\;

178  filename files pipe "dir  /b &path.*PPI.txt";

179  data want(drop=n);


180    infile files truncover;


181    informat ppi_alto $20.;


182    informat date ddmmyy10.;


183    informat time time8.;


184    format date ddmmyy10.;


185    format time time8.;


186    informat Participant_Group $10.;


187    informat Participant_Name $17.;


188    informat Block_Name $12.;


189    informat Trial_Name $12.;


190    informat Event_Name $27.;


191    informat Participant_response $28.;


192    informat key $1.;


193    informat pressed_or_released $7.;


194    informat correct_response $28.;


195    informat error_code $2.;


196    informat reaction_time best32.;


197


198    informat v1 $1.;


199    informat v2 $1.;


200    informat v3 $1.;


201    informat v4 $1.;


202    informat v5 $1.;


203    retain  ppi_alto date time;


204    input fname $80.;


205    fname=cats("&path.",fname);


206    infile dummy filevar=fname dlm='09'x dsd truncover end=done;


207    n=0;


208    do while(not done);


209      n+1;


210      if n eq 1 then do;


211        input /ppi_alto/date time///;


212      end;


213      else do;


214        input Participant_Group Participant_Name Block_Name Trial_Name


215        Event_Name Participant_response key pressed_or_released


216        correct_response error_code reaction_time v1-v5;


217        output;


218     end;


219    end;


220   run;



NOTE: The infile FILES is:


      Unnamed Pipe Access Device,


      PROCESS=dir  /b C:\temp\Mexico123\*PPI.txt,


      RECFM=V,LRECL=256



Stderr output:


There is not enough space on the disk.


NOTE: 0 records were read from the infile FILES.


NOTE: The data set WORK.WANT has 0 observations and 19 variables.


NOTE: DATA statement used (Total process time):


      real time           0.05 seconds


      cpu time            0.03 seconds

PROC Star
Posts: 7,364

Re: How can I convert the attached txt file into the attached sas file?

Linlin:  Your solution is here: http://support.sas.com/kb/41/863.html

Super Contributor
Posts: 1,636

Re: How can I convert the attached txt file into the attached sas file?

Thank you Art!!!

Super User
Posts: 9,687

Re: How can I convert the attached txt file into the attached sas file?

Another way is to use Call execute();

data x;

input x : $100. ;

cards;

C:\temp\forum\folder2\CC0001-V18-190810-1.txt

C:\temp\forum\folder2\CC0001-V18-190810-2.txt

C:\temp\forum\folder2\CC0001-V18-190810-3.txt

C:\temp\forum\folder2\CC0001-V18-190810-4.txt

;

run;

data _null_;

set x;

call execute(' data _'||strip(_n_)||';

                    infile " '||strip(x)||' " dsd dlm="|" lrecl=10000; ' ||

                      ' input a : $10. b : yymmdd10.; run;'   );

run;

data want;

set _: ;

run;

Ksharp

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 315 views
  • 9 likes
  • 3 in conversation