Dear all,
I have hundreds of similar txt files. What is the best way to convert them into one SAS dataset?
Thanks in advance!
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;
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;
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
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.
Thank you Art!!! I changed the length to $80 and the problem is gone. - Linlin
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
Linlin: Your solution is here: http://support.sas.com/kb/41/863.html
Thank you Art!!!
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
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.