Hi All,
It is required to read input file (can be a log file or text file or other) which contains blanks lines, lines with % and & characters, lines without these % and & characters. Need to
1. Check that the input file exists or not
2. if input file exists then read it and write the contents to the output file.
3. Output file name need to be inputfile_out.extension
Please, help. As of now, I don't have any sample input file and hence not able to attach it.
@Moksha wrote:
To get familiar using SAS macro with the reading of complex files containing special characters % and & and write it to the output file.
Don't.
Use the macro to generate the SAS code that can read the file. The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.
%macro check_file(infile);
%local loc outfile;
%if 0=%length(&infile) %then %put ERROR: No filename provided.;
%else %if %sysfunc(fileexist(&infile)) %then %do;
%let loc=%sysfunc(findc(&infile,.,-%length(&infile)));
%if ^(&loc) %then %let outfile=&infile._out;
%else %let outfile=%qsubstr(&infile,1,&loc-1)_out.%qsubstr(&infile,&loc+1);
data _null_;
infile "&infile";
file "&outfile";
input;
put _infile_;
run;
%end;
%else %put ERROR: &=infile is not found.;
%mend check_file;
Please provide an example, also what you have tried so far.
I have tried the following code:
%macro check_file(infile);
%if %sysfunc(fileexist(&infile)) %then %do;
%let filrf = myfile;
%let rc = %sysfunc(filename(filrf, &infile));
%let infid = %sysfunc(fopen(&filrf, i));
%if &infid > 0 %then %do;
%do %while(%sysfunc(fread(&infid) = 0);
%let rc = sysfunc(fget(&infid, mystring, 32767));
%if %sysfunc(indexc(%superq(mystring), %^&)) > 0 %then %do;
%let mystring1 = %sysfunc(tranwrd(%superq(mystring), %, ~));
%let mystring1 = %sysfunc(tranwrd(superq(mystring1), &,^));
%put The contents of the file are %superq(mystring1);
%end;
%else %do;
%put The contents of the file are %superq(mystring);
%end;
%end;
%end;
%let rc = %sysfunc(fclose(&infid));
%end;
%else %do;
%put Could not open the file &infile;
return;
%end;
%end;
%else %do;
%put The file &infile does not exist;
%end;
%mend check_file;
Here, I have tried to replace % with ~ and & with ^ as these characters when found in the input file are getting evaluated and giving errors. But, if lines containing % and ^ characters could be written as it is to the output file then it will be very helpful. Also, I have not tried yet the code for writing to the output file, instead I am writing to the log. But, need to write to the output file.
With the above code, I was getting some warnings like when the line is %macro namex(name=, number=); WARNING: Apparent symbolic reference NUMBER not resolved.
When the line is like this: %do n= 1 %to &number; then WARNING: Apparent symbolic reference NAME not resolved. WARNING: Apparent symbolic reference N not resolved.
Why do you have to read the file in and then write it out to a different file? Why can't you simply perform a copy (or copy+rename) in your operating system? You can even call these operating system commands from SAS.
To get familiar using SAS macro with the reading of complex files containing special characters % and & and write it to the output file.
@Moksha wrote:
To get familiar using SAS macro with the reading of complex files containing special characters % and & and write it to the output file.
Ok, but for this task using operating system commands will work and will be much easier.
@Moksha wrote:
To get familiar using SAS macro with the reading of complex files containing special characters % and & and write it to the output file.
Don't.
Use the macro to generate the SAS code that can read the file. The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.
%macro check_file(infile);
%local loc outfile;
%if 0=%length(&infile) %then %put ERROR: No filename provided.;
%else %if %sysfunc(fileexist(&infile)) %then %do;
%let loc=%sysfunc(findc(&infile,.,-%length(&infile)));
%if ^(&loc) %then %let outfile=&infile._out;
%else %let outfile=%qsubstr(&infile,1,&loc-1)_out.%qsubstr(&infile,&loc+1);
data _null_;
infile "&infile";
file "&outfile";
input;
put _infile_;
run;
%end;
%else %put ERROR: &=infile is not found.;
%mend check_file;
@Tom wrote:
@Moksha wrote:
To get familiar using SAS macro with the reading of complex files containing special characters % and & and write it to the output file.
Don't.
Use the macro to generate the SAS code that can read the file. The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.
I also agree with @Tom
Part of learning how to use the SAS macro language is learning when NOT to use the SAS macro language.
Thank you very much Tom, it is very helpful. Also, thank you very much for the valuable advise.
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.