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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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