BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ManoharNath
Obsidian | Level 7
%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.

1 ACCEPTED SOLUTION

Accepted Solutions
ManoharNath
Obsidian | Level 7

Thanks you, I was able to solve the problem in two steps, first combining and later on creation of flag.

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
ManoharNath
Obsidian | Level 7

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.

 

 

maguiremq
SAS Super FREQ

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.

ManoharNath
Obsidian | Level 7

I tried some codes , its working while combing all the .sas files but unable to do another task which is finding strings 

 

maguiremq
SAS Super FREQ

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.

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ManoharNath
Obsidian | Level 7

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.

Quentin
Super User

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.

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ManoharNath
Obsidian | Level 7

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. 

ManoharNath
Obsidian | Level 7

Thanks you, I was able to solve the problem in two steps, first combining and later on creation of flag.

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
andreas_lds
Jade | Level 19

Following Maxim 14, i would use "find in files" in notepad++ ...

 

ManoharNath
Obsidian | Level 7

its completed and got done in two steps. Thank you. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1769 views
  • 6 likes
  • 6 in conversation