SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
acordes
Rhodochrosite | Level 12

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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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



View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

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



acordes
Rhodochrosite | Level 12

@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. 

 

pic1.png

 

 

yabwon
Onyx | Level 15

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



acordes
Rhodochrosite | Level 12

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;

 

yabwon
Onyx | Level 15

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



sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2468 views
  • 2 likes
  • 2 in conversation