BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmin87
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1881 views
  • 1 like
  • 2 in conversation