Concatenating several files into one in order to print faster

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

Concatenating several files into one in order to print faster

Hi,

 

I  have a testing folder named "concat" containing 3 text files conc1-conc3. What I would lie to do is to use SAS to conctenate these 3 files into 1 so that I can print once insted of printing 3 times. I found the following macro at  http://www2.sas.com/proceedings/sugi26/p219-26.pdf

 

%macro concat2(dir=, prmptcmd=, catfile=, pagefeed=N);
filename filelist pipe "dir %cmpres(&dir)&prmptcmd";
filename tmp "%cmpres(&dir)%cmpres(&catfile)";
%let rc=%sysfunc(fdelete(tmp));
%if %upcase(&pagefeed)=Y %then %do;
data _null_;
file "%sysfunc(pathname(work))\pagefeed.txt";
pagefeed=byte(12);
put @1 pagefeed;
run;
%end;
data _null_;
length filename $200;
infile filelist end=last;
input filename $;
file "%sysfunc(pathname(work))\concat.bat";
cmd = "type %trim(&dir)"||compress(filename)||"
>> %trim(&dir)%left(&catfile)";
put cmd;
%if %upcase(&pagefeed)=Y %then %do;
if not(last) then do;
cmd = "type
%sysfunc(pathname(work))\pagefeed.txt
>> %trim(&dir)%left(&catfile)";
put cmd;
end;
%end;
if last then do;
put "exit";
call execute('x "%sysfunc(pathname(work))\concat";');
end;
run;
%mend concat2;

 

But when I try to run it with:

%concat2(dir=C:\Documents and Settings\HP_Administrator\Desktop\concat\, prmptcmd=*.txt /b, catfile=allfiles.txt,
pagefeed=Y);

 

I get a message that "the systen can not find the path spedified").

 

I realize that the macro is quite complicated and I still try to understand what its parts mean, but could you please help me find the problem, unless another (and hopefully easier) way exists to get the desired result.

 

Thank you! 


Accepted Solutions
Solution
‎04-06-2016 11:05 PM
Grand Advisor
Posts: 17,356

Re: Concatenating several files into one in order to print faster

Do you have headers in each file? Are those concatenated as well?

 

I would recommend a system command instead. 

In UNIX the command below would concatenate the 3 files into 1. 

cat file1 file2 file3 > file123

In windows the command is type instead of cat.

 

type file1 file2 file3 > file123

I would use %sysexec or X to pass this command to your OS. 

 

View solution in original post


All Replies
Solution
‎04-06-2016 11:05 PM
Grand Advisor
Posts: 17,356

Re: Concatenating several files into one in order to print faster

Do you have headers in each file? Are those concatenated as well?

 

I would recommend a system command instead. 

In UNIX the command below would concatenate the 3 files into 1. 

cat file1 file2 file3 > file123

In windows the command is type instead of cat.

 

type file1 file2 file3 > file123

I would use %sysexec or X to pass this command to your OS. 

 

Super Contributor
Posts: 413

Re: Concatenating several files into one in order to print faster

Hi Reeza,

 

Thank you for replying, I tried the following code but it didn't work:

 

X 'type C:\Documents and Settings\HP_Administrator\Desktop\concat\conc1.txt C:\Documents and Settings\HP_Administrator\Desktop\concat\conc2.txt > C:\Documents and Settings\HP_Administrator\Desktop\concat\conc12.txt'

 

 

I guess that the files must come with their extensions, but the code and several modifications of it didn't work, I guess that its becasue I have some syntax error, would greatly appreciate if you helped me find out where it is! Smiley Happy

 

Grand Advisor
Posts: 17,356

Re: Concatenating several files into one in order to print faster

When you have spaces in your paths you need to include the file names in quotes. I suggest getting it working in windows and then porting that command to SAS. 

Super Contributor
Posts: 413

Re: Concatenating several files into one in order to print faster

[ Edited ]

Hi,

 

I included the double quotes:

X 'type "C:\Documents and Settings\HP_Administrator\Desktop\concat\conc1.txt" "C:\Documents and Settings\HP_Administrator\Desktop\concat\conc2.txt" > "C:\Documents and Settings\HP_Administrator\Desktop\concat\allfiles.txt"'

 

But I still don't get anything, and what is interesting is that there is no error message, and the command prompt also doesn't open.

 

When I typed directly into command prompt it actually worked and the files got concatenated, but when I went back to SAS it still didn't work...

 

Thanks 

 

 

Esteemed Advisor
Posts: 6,661

Re: Concatenating several files into one in order to print faster

Another method to run external commands is the filename pipe method:

filename oscmd pipe 'systemcommand 2>&1';

data _null_;
infile oscmd;
input;
put _infile_;
run;

This will give you all messages/output from the systemcommand in the SAS log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 413

Re: Concatenating several files into one in order to print faster

Hi KurtBremser,

 

I ran the code but got a message that 'systemcommand' is not recognized as an internal or external command, operable eprogram or batch file. 

Is the part 

'systemcommand 2>&1';

a universal code or should it be particular to my computer and as such I wrote the wrong code?

 

thank you!

 

 

Esteemed Advisor
Posts: 6,661

Re: Concatenating several files into one in order to print faster

I used "systemcommand" as a stand-in for any operating system command you want to run. Couldn't italicize it in the SAS code window.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 17,356

Re: Concatenating several files into one in order to print faster

Try %sysexec instead of X

 

Super Contributor
Posts: 413

Re: Concatenating several files into one in order to print faster

Hi Reeza,

 

now the method with X is working!

 

I don't know why it didn't work the other time, unless I made a typo...

 

 

Thanks!

Super Contributor
Posts: 413

Re: Concatenating several files into one in order to print faster

Hi again Reeza,

 

my text files are now getting concatenated when I do X' type "C:\file1.txt" "C:\file2.txt" > "C:\file3.txt" ';

 

But I noticed something weired:

when I want to concatenate all of the text files in a folder: X' type "C:\*.txt" > "C:\file4.txt" ';

the first file (file 1.txt) is actually printed 2 times into the new file4.txt?

 

 

Thank you! 

Grand Advisor
Posts: 17,356

Re: Concatenating several files into one in order to print faster

No idea - that's a Windows forum issue. 

 

You confirm that that you only have one file to start as well as making sure to run it from scratch to avoid any issues with previous runs. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 531 views
  • 5 likes
  • 3 in conversation