Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse system command data 1

Status
Not open for further replies.

nickdel

Programmer
May 11, 2006
367
GB
Not sure if this is possible but can I parse the results of the following into a dataset?

x 'find /c "searchstr" "x:\sas\data\*.sas"';

basically all this does is call the DOS "find" command. The idea of the program is that it will search sas program files for a particular word or phrase (windows search cannot do this but the dos command can, dont figure!)

The above command works fine, just not sure how to then get the data into a useful format.

Cheers

Nick

where would we be without rhetorical questions...
 
ok, I actually figured this out but now hoping some macro guru can now help me out...!

Code:
filename Results pipe 'command/c find /c /i "incremental" "x:\core sas\data\*.sas"';

data Results ;

     infile Results length=recLen;
 
     input buffer $varying256. reclen;
	
	 OccurancesInFile = scan(buffer,-1,':');

	if OccurancesInFile > 0 then do;
	 	FilePath = scan(buffer,-1,'\');
		File = scan(FilePath,1,':');
		drop buffer FilePath;
		output;
	end;

run;

The above basically does what I wanted from the original post and works pretty well for anyone wishing to use the code!

Ideally I would like to put this into a macro however I've yet to attend that course and have built these purely by guess work in the past. Here's what I've been trying but to no avail. Hopefully someone can see where I'm going wrong!

Code:
%macro SearchInFile(SearchString, SearchPath);

Options noxwait noxsync;

%let SearchIn = %str(&SearchPath.*.sas);

filename Results pipe "%str(command/c find /c /i %"&SearchString%" %"&SearchIn%")";

data Results ;

     infile Results length=recLen;
 
     %input buffer $varying256. reclen;
	
	 OccurancesInFile = scan(buffer,-1,':');

	%if OccurancesInFile > 0 %then %do;
	 	FilePath = scan(buffer,-1,'\');
		File = scan(FilePath,1,':');
		drop buffer File;
		output;
	%end;

run;

%mend SearchInFile;

%SearchInFile(incremental,x:\core sas\nick\);

Thanks

Nick

where would we be without rhetorical questions...
 
Hi Nickdel,

I'm no macro guru, but I'll offer some suggestions. In your example, you only really seem to need to use a macro to encapsulate your code - with exception of passing macro variables. One of the main things about understanding the macro system, is that executes before sas code (in the same data step), so the use of %if (condition) actually executes before the condition is defined. Apart from that you had it pretty much spot on :). HTH

Code:
%macro SearchInFile(SearchString, SearchPath);

Options noxwait noxsync;

%let SearchIn =&SearchPath.*.sas;

filename Results pipe "%str(command/c find /c /i %"&SearchString%" %"&SearchIn%")";

data Results ;

     infile Results length=recLen;
 
     input buffer $varying256. reclen;
    
     OccurancesInFile = scan(buffer,-1,':');

    if OccurancesInFile > 0 then do;
        FilePath = scan(buffer,-1,'\');
        File = scan(FilePath,1,':');
        drop buffer File;
        output;
    end;

run;

%mend SearchInFile;
 
Also, I think that this line:-
Code:
filename Results pipe "%str(command/c find /c /i %"&SearchString%" %"&SearchIn%")";

Might need to be
Code:
filename Results pipe "command/c find /c /i "&SearchString" "&SearchIn")";
I don't think you need all the extra % signs etc.

As a side note, it's useful when listing code that didn't work, to explain how it didn't work, ie, did the code produce results, but not the results you wanted? Did it produce error messages? etc etc.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top