I have two text files or maybe more two but for this example, two text files. I would like the code to capture two or more text files, sort them by the numbers in the first column before the | then and combine the data in the multiple text files then output it to one text files with the same format with the first column or numbers sorted. How would I write a code to tackle this?
Are you running on Unix? Can you call operating system commands? If so I would just use CAT to concatenate the files and SORT to sort the result.
If you are forced to use SAS code only then read the lines and sort by the first field. From your examples the first field is text.
filename in ('TEST1.txt','TEST2.txt');
data all ;
infile in truncover dsd dlm='|';
input id :$20. ;
length line $4000 ;
line=_infile_;
run;
proc sort data=all;
by id ;
run;
data _null_;
file 'newfile.txt' ;
set all;
len = lengthn(line);
put line $varying4000. len;
run;
Change the 4000 length for the LINE variable to whatever makes sense for your actual files.
Are you running on Unix? Can you call operating system commands? If so I would just use CAT to concatenate the files and SORT to sort the result.
If you are forced to use SAS code only then read the lines and sort by the first field. From your examples the first field is text.
filename in ('TEST1.txt','TEST2.txt');
data all ;
infile in truncover dsd dlm='|';
input id :$20. ;
length line $4000 ;
line=_infile_;
run;
proc sort data=all;
by id ;
run;
data _null_;
file 'newfile.txt' ;
set all;
len = lengthn(line);
put line $varying4000. len;
run;
Change the 4000 length for the LINE variable to whatever makes sense for your actual files.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.