%macro list_files(dir,ext); %local filrf rc did memcnt name i; %let rc=%sysfunc(filename(filrf,&dir)); %let did=%sysfunc(dopen(&filrf)); %if &did eq 0 %then %do; %put Directory &dir cannot be open or does not exist; %return; %end; %do i = 1 %to %sysfunc(dnum(&did)); %let name=%qsysfunc(dread(&did,&i)); %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do; %put &dir\&name; data _tmp; length dir $512 name $100; dir=symget("dir"); name=symget("name"); full_path = catx('\',dir,name); run; proc append base=want_new data=_tmp; run;quit; %end; %else %if %qscan(&name,2,.) = %then %do; %list_files(&dir\&name,&ext) %end; %end; %let rc=%sysfunc(dclose(&did)); %let rc=%sysfunc(filename(filrf)); %mend list_files; %list_files(\\path,sas)
I am trying to read all directory and its subdirectory for .SAS (SAS extension) files and then find specific string and create columns as file path and file name and string flag name for a specific string like datamart used in code and then create flag and assign value as 1 if its getting used within code and output as table.
Thanks you, I was able to solve the problem in two steps, first combining and later on creation of flag.
@ManoharNath wrote:
read all directory and its subdirectory for .SAS (SAS extension) files and then find specific string and create columns as file path and file name and flag name for a specific string like datamart used in code and create columns flag and assign value as 1 if its getting used within code and output as table.
This looks like a homework assignment. Most of us will not do your homework for you, but if you show us the code you have written and point out what problems you have, we would be glad to help.
First step: I am trying to read all .SAS files which sits in all directory and its subdirectory
2nd step: then Create a flag for particular string for like datamart word which are written any codes.
3rds step: then create columns like file path and file name and String name and string flag value as 1 if its getting used in code.
There is a pinned tab on the main page of the SAS Programming forum that discusses this. You may have to modify some things, but it should get you started.
I tried some codes , its working while combing all the .sas files but unable to do another task which is finding strings
Okay, echoing @PaigeMiller, do you have any code you can post? Can you also post your log of what isn't working?
For code, click the insert sas code icon in your post and paste it in. For your log, you can click the "insert code" icon (</>) and paste it in there.
As Paige asked, please show the post the code you have tried that is not working, describe how it is not working (wrong results? errors?) and post the log as well. If you're stuck on the first step, you can post just the first step. And even folks here will either help you update the code, or point you toward alternative approaches.
above code working for me to find all sas files and combines in output table and apologies I am not sure how to find for string within those codes as I am not sure how to do that.
Do you know how to write a DATA step that will read in a text file with the input statement?
A .sas program is just a txt file. So you can read it in as data, one line at a time, and use the FIND() function or similar to see if a word is on the line. So you could process one .sas file like:
data want ;
infile "Q:\junk\sleep.sas" end=last;
input ;
retain found 0;
if find(_infile_,"datetime") then found=1 ;
if last then output ;
run ;
That macro you posted for directory crawling looks like a good utility macro. But if you're learning SAS, you're probably better off writing your own code for each step rather than using tools developed by others. The macro language is very useful to know, but it also adds a layer of complexity that you probably don't want when you're learning the SAS language.
able to solve problem in two step, after some hour of testing and it got done.
I know SAS very well and I was hoping if some one can share some ideas because as I was missing on few points but any way know no one here is to share ideas or thoughts.
Thanks you, I was able to solve the problem in two steps, first combining and later on creation of flag.
Glad you got it solved. It's ok to select your own answer as the correct solution, but that is only useful to the community if you include the solution in your answer. I hope you'll consider updating your answer to include the code that worked for your need. I'm curious to see how it differs from the solution @Reeza linked to.
Following Maxim 14, i would use "find in files" in notepad++ ...
Does the solutions posted here not work for you?
its completed and got done in two steps. Thank you.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.