In SAS Viya 3.5 SAS Studio I make use of projects to save my SAS codes in an organized way. Furthermore I create code snippets for partial tasks I use frequently.
Nevertheless sometimes I have a 'dejà vue' when facing a new challenge. It goes like "Arne, you solved this once using a specific cas action 'transpose'."
Now I would like to (regex) search for 'transpose' in all my sas codes.
But the built-in seach-in function is very rudimentary.
So my idea is to write all the sas code to a txt file.
My starting points:
Thanks
lets assume the following setup:
/*lets assume the following setup*/
filename f "R:\folder with my sas codes\";
data _null_;
file f(code1.sas);
put "data test;" / "x = 17; output;" / "x = 42; output;" / "run;";
file f(code2.sas);
put "proc sort data = test;" / "by x;" / "run;";
file f(code3.sas);
put "proc transpose data = test out = transposed_data;" / "var x;" / "run;";
run;
/* test if they work */
%include f(code1.sas);
%include f(code2.sas);
%include f(code3.sas);
Is this:
/* searching */
filename search "R:\folder with my sas codes\*";
%let myStr = proc; /* string you are looking */
data result;
length file $ 256 code_line $ 2048;
infile search filename = file end=end;
input;
lineNumber+1;
filename = file;
code_line= _infile_;
if find(_infile_, "&myStr.", "i") then output;
if end then lineNumber=0;
run;
proc print data=result;;
run;
a solution you are looking for?
Bart
Hi @acordes ,
Are your files with code stored in one place e.g., a folder like: /Deskto/myCodes/*.sas ?
Bart
@yabwon yes.
For example I would like to export / append all sas codes in the specified project folder to a txt file.
The code of the image has nothing to do with my question.
It serves only to share with you my folder path and how I use it for other tasks.
lets assume the following setup:
/*lets assume the following setup*/
filename f "R:\folder with my sas codes\";
data _null_;
file f(code1.sas);
put "data test;" / "x = 17; output;" / "x = 42; output;" / "run;";
file f(code2.sas);
put "proc sort data = test;" / "by x;" / "run;";
file f(code3.sas);
put "proc transpose data = test out = transposed_data;" / "var x;" / "run;";
run;
/* test if they work */
%include f(code1.sas);
%include f(code2.sas);
%include f(code3.sas);
Is this:
/* searching */
filename search "R:\folder with my sas codes\*";
%let myStr = proc; /* string you are looking */
data result;
length file $ 256 code_line $ 2048;
infile search filename = file end=end;
input;
lineNumber+1;
filename = file;
code_line= _infile_;
if find(_infile_, "&myStr.", "i") then output;
if end then lineNumber=0;
run;
proc print data=result;;
run;
a solution you are looking for?
Bart
Thanks Bart.
Only for completeness, I had to wrap your solution into a data null & call execute after writing the files in the folder to a file.
https://communities.sas.com/t5/SAS-Programming/How-to-List-all-the-files-in-a-folder/td-p/674065
Credits to @Kurt_Bremser
I think it's sas viya specific how it can access path locations.
But it works and does exactly what I was looking for.
😊
filename myfldr filesrvc folderPath='/Projects/Portugal/';
data filenames;
length fname $200;
did = dopen('myfldr');
do i = 1 to dnum(did);
fname = dread(did,i);
output;
end;
did = dclose(did);
keep fname;
run;
%let myStr = symputx; /* string you are looking */
data _null_;
set filenames;
if index(lowcase(fname), '.sas') then
call execute ('
filename search FILESRVC FOLDERPATH="/Projects/Portugal/" FILENAME=' || quote(strip(fname)) || ' ;
data temp;
length file $ 256 code_line $ 2048;
infile search filename = file end=end;
input;
lineNumber+1;
filename = file;
code_line= _infile_;
if find(_infile_, "&myStr.", "i") then output;
if end then lineNumber=0;
run;
proc append base=result data=temp;
run;
');
run;
You could make:
if scan(lowcase(fname),-1,".") = 'sas' then output;
in the first datastep to make filtering faster.
Bart
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.