Find SAS programs present in a folder ,having keyword 'Finance' inside it

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Find SAS programs present in a folder ,having keyword 'Finance' inside it

Hello All,

 

Warm greetings.

I appreared for a SAS developer interview recently ,and interviewer asked me this question .

 

Question:

There is a folder in windows directory system.

it has many SAS programs,which may or may not contain keyword 'Finance' .

Write a SAS program to find out all such SAS programs from the folder.

X command utility cannot be used for this.

 

Please advise .

Many thanks in advance for all the inputs.

 

Kind regards,

Neeraj Singh

 

 


Accepted Solutions
Solution
‎05-11-2017 10:34 AM
Super User
Posts: 1,230

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

You can use next code to serach for text files, including sas programs or logs, containing any desired given string:

 

%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&suffix"); 

data results;
     length fname _filepath $200;
     infile finp filename = _filepath eov=_eov truncover;
     input a_line $200.;
     fname = _filepath;
     
     if _eov=1 then do;
       _n=0;

       _eov=0;
     end;
     _n+1;
     
     if find(a_line,"&search_string",'i')
     then output;
     keep _n a_line fname;
run;     
   

If you are not using linux platform you may need adapt it to your OS.

View solution in original post


All Replies
Grand Advisor
Posts: 17,331

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

What was your answer?

Occasional Contributor
Posts: 9

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

I did not give any answer. I was only able to think of X command utility and firing up a DOS command inside it.

but using SAS, i was speechles.. !! :'( 

Grand Advisor
Posts: 17,331

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it


thisisneeraj wrote:

I did not give any answer. I was only able to think of X command utility and firing up a DOS command inside it.

but using SAS, i was speechles.. !! :'( 


FYI - That's part of the 'question' how do you respond to things you don't know the answer to, the interviewers want that information.

 

If you don't know the correct answer is, "I don't know but I would do X, Y, Z to find out". Or "My first option would be use an OS tool, but if I had to use SAS I would try reading the file in and searching manually, but the processing time would be much longer. I know this isn't an efficient method, but it would work".

 

Another option is using something like Windows Search.  Unless the job was for an advanced programmer that's what I would be looking for at any rate. 

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

Yes good point made by @Reeza here.  Often questions are designed to see if you can do something, not necessarily how you do things.  Quite often we see questions on here which start with something like "I have to use xyz".  Knowing how to use a tool is one thing, knowing when to use a tool is another.  I mean I could ask, "You are only allowed to use paint, please explain how you would write a report".  Now you could fix on SAS and say well I would write a datastep which creates an array of pixels, thus drawing the commands to create the picture.  Or you could say, why not use an application designed specifically create to create pictures, there are many out there varying costs, knowledge levels, lets do an assessment on how one of those could integrate into the currentflow best.

 

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

They do ask some tosh don't they.  Anyways, you can do it with DOPEN and DREAD:

http://support.sas.com/documentation/cdl/en/hostwin/69955/HTML/default/viewer.htm#n1xxcn6l9bttm4n1em...

 

 

Solution
‎05-11-2017 10:34 AM
Super User
Posts: 1,230

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

You can use next code to serach for text files, including sas programs or logs, containing any desired given string:

 

%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&suffix"); 

data results;
     length fname _filepath $200;
     infile finp filename = _filepath eov=_eov truncover;
     input a_line $200.;
     fname = _filepath;
     
     if _eov=1 then do;
       _n=0;

       _eov=0;
     end;
     _n+1;
     
     if find(a_line,"&search_string",'i')
     then output;
     keep _n a_line fname;
run;     
   

If you are not using linux platform you may need adapt it to your OS.

Valued Guide
Posts: 2,174

Re: Find SAS programs present in a folder ,having keyword 'Finance' inside it

Could be simpler.
The required string was defined in mixed case so I expect it calls for a case sensitive search. The INPUT statement makes this really brief:
Data required_files( compress=yes) ;
Length filename filen $1000 ;
Retain filename ;
Infile "&folder/*.sas" filename= filen lrecl= 32000 ;
INPUT @'Finance' ;
If filename= filen then delete ; * found earlier ;
filename= filen ;
Run ;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 139 views
  • 1 like
  • 5 in conversation