DATA Step, Macro, Functions and more

create 2 text files, append them and then sort them

Reply
Contributor
Posts: 25

create 2 text files, append them and then sort them

Each file would have a different number of columns. We would want to sort by the id column. Is this possible in 9.4?

 

 

data file1;
input @1 id 1. @3 start_date mmddyy10.;
format adm_date mmddyy10.;
cards;
1 03/18/2014 
1 03/27/2014
1 03/24/2014
2 03/19/2014
3 03/24/2014
3 03/25/2014

;
run;


data file2;
input @1 id 1. @3 proc_code $5. @9 app_units 1. @11 request_units 1.;
/*format start_date end_date mmddyy10.;*/
cards;
1 J5563 2 4
1 J5563 3 1
2 J3363 4 2
2 J4789 2 6
3 J8434 2 2 
3 J4444 5 5 
;
run;

1 03/18/2014 
1 03/27/2014
1 03/24/2014
1 J5563 2 4
1 J5563 3 1
2 03/19/2014
2 J3363 4 2
2 J4789 2 6
3 03/24/2014
3 03/25/2014
3 J8434 2 2
3 J4444 5 5








Super User
Posts: 10,527

Re: create 2 text files, append them and then sort them

Your example output tends to imply that you have combined the two datasets and are exporting to one file, not two.

 

This creates a data set with contents that look like your output. I have removed the blank line generated in your data step for file1.

The OPTIONS MISSING=' '; is so that the bit that combines all of the variables in the temporary set don't get . The catx places a single space between all of the non-missing values that way. If your real data has more variables you may need to increase the length of the Line variable to accomodate more or longer variables.

data file1;
input @1 id 1. @3 start_date mmddyy10.;
format adm_date mmddyy10.;
cards;
1 03/18/2014 
1 03/27/2014
1 03/24/2014
2 03/19/2014
3 03/24/2014
3 03/25/2014
;
run;


data file2;
input @1 id 1. @3 proc_code $5. @9 app_units 1. @11 request_units 1.;
/*format start_date end_date mmddyy10.;*/
cards;
1 J5563 2 4
1 J5563 3 1
2 J3363 4 2
2 J4789 2 6
3 J8434 2 2 
3 J4444 5 5 
;
run;

options missing=' ';
data temp;
   set file1 file2;
   length line $ 200;
   line= catx(' ',of _all_);
run;
proc sort data=temp out=want (keep=line);
   by id;
run;

proc export data=want out='path/file.txt';
run;

options missing='.';


Contributor
Posts: 25

Re: create 2 text files, append them and then sort them

Thanks so much for the reply. It looks like a winner.

Contributor
Posts: 25

Re: create 2 text files, append them and then sort them

After looking at this closer, I don't think it works. Here is the output.

 

1 19800
1 19809
1 19806
1 J5563 2 4
1 J5563 3 1
2 19801
2 J3363 4 2
2 J4789 2 6
3 19806
3 19807

 

It combines the start_date and proc_code columns into one column and they should be two separate columns.
3 J8434 2 2
3 J4444 5 5

Contributor
Posts: 25

Re: create 2 text files, append them and then sort them

I took out the options missing=' '; line and it looks like that may work. It preserves each column. I'll have to look at the actual data but I think this might work. Thanks for the help.

Ask a Question
Discussion stats
  • 4 replies
  • 224 views
  • 0 likes
  • 2 in conversation