- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
- the proc printto but I only know to use it for the log files.
- Or the data _null_ with put and file statement but I only know how to execute a sas file with the %include statement, but not to read or print a sas code file
Thanks
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @acordes ,
Are your files with code stored in one place e.g., a folder like: /Deskto/myCodes/*.sas ?
Bart
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could make:
if scan(lowcase(fname),-1,".") = 'sas' then output;
in the first datastep to make filtering faster.
Bart
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation