***************************************************************************************************************l /* Directory Tools Unix and Windows */ /* these two tools work on all platforms */ /* put all filenames in C:\Program Files\SAS\SAS 9.1 with '.sas' into c:\temp\log.txt */ %let pgm=utl_dirwalk; %macro dirwalk(fname, filter=%str(.sas) ); %local i filrf rc did memcnt; %let rc=%sysfunc(filename(filrf,&fname)); %let did=%sysfunc(dopen(&filrf)); %if &did > 0 %then %do; * seems to be a directory, so walk it; %let memcnt=%sysfunc(dnum(&did)); %if &memcnt > 0 %then %do i=1 %to &memcnt; %let name=&fname\%sysfunc(dread(&did,&i)); %dirwalk(&name,filter=&filter); %end; %let rc=%sysfunc(dclose(&did)); %end; %else %do; %if %index(&fname,%str(&filter)) ne 0 %then %put &fname; %end; %mend dirwalk; proc printto log="c:\temp\log.txt" new;run; %dirwalk(C:\Program Files\SAS\SAS 9.1,filter=%str(.sas)); proc printto;run; /* put all lines within all files in dir and subdir that have "remote" into c:\temp\log.txt */ %macro findstr(fname, filter=%str(remote), fout=%str(c:\temp\dotsas.txt) ); %local i filrf rc did memcnt; * Try to open this fname as a directory; %let rc=%sysfunc(filename(filrf,&fname)); %let did=%sysfunc(dopen(&filrf)); %if &did > 0 %then %do; * seems to be a directory, so walk it; %let memcnt=%sysfunc(dnum(&did)); %if &memcnt > 0 %then %do i=1 %to &memcnt; %let name=&fname\%sysfunc(dread(&did,&i)); %findstr(&name,filter=&filter); %end; %let rc=%sysfunc(dclose(&did)); %end; %else %do; data _null_; retain cnt 0; infile "&fname"; input; file "&fout" mod; _infile_=left(_infile_); if index(_infile_, "&filter")>0 then do; cnt=cnt+1; if cnt=1 then put / "&fname" / _n_ z4. @11 _infile_; else put _n_ z4. @11 _infile_; end; run; %end; %mend findstr; %utlfkil(c:\temp\dotsas.txt); /* macro is below */ %findstr(C:\Program Files\SAS\SAS 9.1\connect,filter=%str(remote), fout=%str(c:\temp\dotsas.txt)); /* echo back to the sas output window the results of any unix command */ /* put in autocall library */ /* ie xos "ls -l"; %macro xos(afstr1)/cmd; /* usage xos "ls -l" */ %syslput afstr1=&afstr1; note;notesubmit '%xosa'; %mend xos; %macro xosa; rsubmit; options nonotes; filename xlstcmd pipe &afstr1; data _null_; file print; infile xlstcmd; input; put _INFILE_; run; options notes; endrsubmit; %mend xosa; /* echo back to the sas output window the results of any widows command command */ /* ie xos "ls -l"; %macro dos(afstr1)/cmd; /* usage dos "dir" */ note;notesubmit '%dosa'; %mend dos; %macro dosa; options nonotes; filename xlstcmd pipe &afstr1; data _null_; file print; infile xlstcmd; input; put _INFILE_; run; endrsubmit; %mend dosa; /* member names of one directory into sas dataset */ %macro utl_dirmem(dir=/utl/molecule/sas/meta/datamart/data/error,sd1=memlst); /* put a list of members in a sas dataset */ data &sd1(keep=name dir); length dir $255 name $64; rc=filename("mydir","&dir"); dir="&dir/"; did=dopen("mydir"); if did > 0 then do; memcount=dnum(did); do i=1 to memcount; name=dread(did,i); output; end; end; run; title "dir &dir - output dataset=&sd1 - variable name"; proc print;; run; title;footnote; %mend utl_dirmem; %utlfkil(c:\temp\dotsas.txt); %findstr(C:\Program Files\SAS\SAS 9.1\connect,filter=%str(remote), fout=%str(c:\temp\dotsas.txt)); USEFUL UNIX COMMANDS list all files and directories recursively find /etc >/tmp/output This will list all files and directories under /etc as a name. If you want files and directories seperated you can do this : find /etc -type d >/tmp/output_directory find /etc -type f >/tmp/output_files Or if you want a ls -l listing of each file : find /etc -type f -exec ll {} \; >/tmp/long_outp_f find /etc -type d -exec ls -lad {} \; >/tmp/long_outp_d copy all *.log files from all directories including subdirectories to the one directory cd /home/rdeangel && find . -name '*.log' -print | pax -rwdpe /home/rdeangel/etc copy all *log files from parent and all subdirectories to one directory find /utl/sas/onc/20010203 -type f -name "*.log" | xargs -I {} cp "{}" /home/rdeangel/etc /* windows directory information into sas dataset */ data tC; length dir command $128; dir = '.\*.txt'; command = catx(' ','DIR /TC /Q /S',quote(strip(dir)),'2>&1'); putlog 'NOTE: ' command=; infile dummy pipe filevar=command end=eof truncover; do until(eof); input @; if index(_infile_,'Directory of') then input @ 'Directory of ' dir $128.; else do; input creation ?? mdyampm18. @; if not missing(creation) then do; input @26 size:??comma16. @40 owner:$32. @63 name$64.; output; end; else input; end; put _infile_; end; _error_ = 0; stop; format creation datetime.; run; proc print; run; omparing files using dos commands %sysexec 'fc "C:\tmp\file 1.sas" "C:\tmp\file 2.sas" > "C:\tmp\compare.txt"'; %put sysrc=&sysrc; For example, here is a DATA step that uses the UNIX System Services od command to write the contents of the file in hexadecimal format to the UNIX System Services file dat/dump.dat, as follows: filename oecmd pipe 'od -x -tc - >dat/dump.dat'; data _null_; file oecmd; input line $ 1-60; put line; cards; The SAS System is an integrated system of software products, enabling you to perform data management, data analysis, and data presentation tasks. ; run; using perl to get datetime in seconds for all objects in a directory on Solaris systems 'ls -l' is unable to express file modification dates in a commmon format with seconds %macro allpth(pth,outlst); data _null_; call system("cd &pth"); call system("setenv PWD &pth"); run; %let pwd=%sysget(PWD); %put pwd=&pwd; filename getdte pipe "perl -e 'foreach(@ARGV){$t =localtime ( ( ( stat ( $_ ) ) [9] ) ); printf(qq{%-64s%s\n},$_,$t);}' *"; data &outlst(index=(program/unique)); infile getdte; input program $64. @69 getmth $3. @73 getday $3. @85 getyer $4. @76 gettym $8.; sav=cats(getday,getmth,getyer,':',gettym); cvtdte=input(cats(getday,getmth,getyer,':',gettym),datetime18.); sdfdtetym=%dtm(cvtdte); fulfyl=cats("&pth",'/',program); if index(program,'.')>0; if not (program =: 'init'); keep program sdfdtetym fulfyl cvtdte; run; filename getdte clear; %mend allpth; %allpth(/utl/molecule/sas/meta/2008_dosesplit/data/output,creout); * set of macros to parse paths getfyl.sas -- given macro variable with full path /home/rdeangel/tst.sas get tst.sas - function macro getstm.sas -- given macro variable with full path /home/rdeangel/tst.sas get /home/rdeangel/ - function macro getfylvar.sas -- given datastep variable with full path /home/rdeangel/tst.sas get tst.sas - function macro getstmvar.sas -- given datastep variable with full path /home/rdeangel/tst.sas get /home/rdeangel/ - function macro data _null_; x='/home/rdeangel/tst.sas'; fyl=%getfylvar(x); put fyl=; stm=%getstmvar(x); put stm=; run; %let pth=/home/rdeangel/tst.sas; %let fyl=%getfyl(&pth); %put fyl=&fyl; %let pth=/home/rdeangel/tst.sas; %let stm=%getstm(&pth); %put stm=&stm; /* Macro */ %macro getfyl(pth); /* extract the file from the full path */ %let revpth=%qleft(%qscan(%qsysfunc(reverse(&Pth)),1,%str(/\))); %let gotfyl=%qleft(%qsysfunc(reverse(&revpth))); %str(&gotfyl) %mend getfyl; /* datastep sql iml */ %macro getfylvar(pth); /* extract the file from the full path */ left(reverse(left(scan(reverse(&Pth),1,'/\')))) %mend getfylvar; /* macro get stem */ %macro getstm(pth); /* extract the path without the file name */ %let revstr=%qleft(%qsysfunc(reverse(&pth))); %let cutstr=%qsubstr(&revstr,%qsysfunc(indexc(&revstr,%str(/\)))); %let gotstm=%qleft(%qsysfunc(reverse(&cutstr))); %str(&gotstm) %mend getstm; /* datastep get stem */ %macro getstmvar(pth); /* extract the path without the filename and extension */ left(reverse(substr(left(reverse(&pth)),indexc(left(reverse(&pth)),'/\')))) %mend getstmvar; data dir; rc=filename("mydir","H:\logs\"); did=dopen("mydir"); if did > 0 then do; memcount=dnum(did); do i=1 to memcount; lstname=dread(did,i); index1 = index(upcase(lstname),".LOG"); if index1 > 0 then do; t_cnt+1; output; end; end; end; run; proc print; var lstname t_cnt; run; let pgm=utl_pypwinzip; /* The syntax parameters for wzunzip are: */ /* */ /* specify the location of the downloaded add-on */ /* -o = overwrite existing file */ /* -c = display contents of file to screen */ /* name of zip file */ /* name of file inside of zip to be read */ filename pipes pipe '"c:\program files\winzip\wzunzip.exe" -o -c c:\sas\data.zip data1.txt'; data test; infile pipes firstobs=9 truncover; input x y $; put x= y=; run; /* extract dates from widows directory listing */ filename dirlst pipe "dir c:\utl\" ; data Dates(keep=raddate trandates trandate2); infile dirlst length=ln end=eof; input all $varying255. ln; if substr(all,1,1) in (1,2,3,4,5,6,7,8,9,0); trandates =(substr(all,1,10)); trandate2 =(substr(all,40,10)); raddate=input(left(trim(substr(trandates,1,2)))||left(trim(substr(trandates,4,2)))|| left(trim(substr(trandates,7,4))),mmddyy8.); run; list_sasautos.sas list all macros in all sasautos and work directory sasmacr and profile conditionally deleting a dataset if it exists - without a macro %sysfunc(ifc(%sysfunc(exist(work.all034))=1, %nrstr(proc datasets library=work;delete all034;run;quit;), %nrstr(%put does not exist;))); or %sysfunc(ifc(%sysfunc(exist(meta.stinfo)),%str(proc datasets library=meta nolist;delete stinfo;run;quit;),%str( ))); list all available formats proc sql; title1 "Available User-Defined Formats"; select libname, fmtname, fmttype, source from dictionary.formats where source not in ("B","U"); quit; list all codes and decodes for all formats in all catalogs in the format search list data _null_; set sashelp.voption(where=(optname='FMTSEARCH')); str=compress(setting,'()'); fmtnum=countc(strip(str),' ') + 1; do i=1 to fmtnum; cmd=cats('title "',scan(str,i,' '),'";','proc format fmtlib library=',scan(str,i,' '),';run;quit;'); call execute(cmd); end; stop; run; copy all *.log files from all directories including subdirectories to the one directory cd /home/rdeangel && find . -name '*.log' -print | pax -rwdpe /home/rdeangel/etc copy all *log files from parent and all subdirectories to one directory find /usr/utl -type f -name "*.log" | xargs -I {} cp "{}" /home/rdeangel/etc /* make a symbolic link in unix */ %macro mkelnk(vue,src); %let lnk=/utl/molecule/sas/meta/datamart/trialdata/views; data _null_; call system("cd &lnk"); call system("setenv PWD &lnk"); run; %sysexec /bin/rm -f &vue; %put sysrc=&sysrc; %sysexec ln -s &src &vue; %put sysrc=&sysrc; %mend mkelnk; %mkelnk(demog.ssas,/utl/molecule/sas/sd970144/analysis/final/utldata/crt/demog.ssas); Searches for strings in files. WINDOWS 2000 FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [strings] [[drive:][path]filename[ ...]] /B Matches pattern if at the beginning of a line. /E Matches pattern if at the end of a line. /L Uses search strings literally. /R Uses search strings as regular expressions. /S Searches for matching files in the current directory and all subdirectories. /I Specifies that the search is not to be case-sensitive. /X Prints lines that match exactly. /V Prints only lines that do not contain a match. /N Prints the line number before each line that matches. /M Prints only the filename if a file contains a match. /O Prints character offset before each matching line. /P Skip files with non-printable characters /A:attr Specifies color attribute with two hex digits. See "color /?" /F:file Reads file list from the specified file(/ stands for console). /C:string Uses specified string as a literal search string. /G:file Gets search strings from the specified file(/ stands for console). /D:dir Search a semicolon delimited list of directories strings Text to be searched for. [drive:][path]filename Specifies a file or files to search. Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y. Regular expression quick reference: . Wildcard: any character * Repeat: zero or more occurances of previous character or class ^ Line position: beginning of line $ Line position: end of line [class] Character class: any one character in set [^class] Inverse class: any one character not in set [x-y] Range: any characters within the specified range \x Escape: literal use of metacharacter x \ Word position: end of word For full information on FINDSTR regular expressions refer to the online Command Reference. C:\Documents and Settings\rdeangel.AM> grepping all SAS logs and SAS programs for variable definitions data _null_; length cmd $300; cmd="grep -i 'itt' /trialdata/*sapcrfsrc/src/*.log > /home/rdeangel/utl/ittlog.txt"; call system(cmd); cmd="grep -i 'itt' /trialdata/*sapcrfsrc/src/*.sas > /home/rdeangel/utl/ittsas.sas"; call system(cmd); run; filename two ( '/home/rdeangel/utl/ittlog.txt' '/home/rdeangel/utl/ittsas.sas' ); data stylyn; retain n 0; length pgm $16 study $8 lyn str cmp $300; infile two; input;; str=_infile_; study=compress(substr(str,44,10),,'DK'); pgm=scan(substr(str,64),1,'.'); if pgm=:'c/' then pgm=substr(pgm,3); lyn=left(substr(str,index(str,':')+1)); lyn=translate(lyn,' ','0A'x); lyn=translate(lyn,' ','09'x); lyn=translate(lyn,' ','0D'x); n+1; keep study pgm lyn n; run; proc sort data=stylyn out=odr nodupkey; by study pgm lyn; run; proc print data=odr width=min; format lyn $120.; var study pgm lyn; run; put all the directories and file pths in a sas dataset libname out "/home/rdeangel/utl"; filename get pipe "find /usr/utl"; data out.can_fyl; infile get; input; infile=_infile_; run; proc sql; select * from out.can_fyl where infile contains 8802 ;quit; %macro utlfkil ( utlfkil ) / des="delete an external file"; %local urc; /*-------------------------------------------------*\ | Open file -- assign file reference | \*-------------------------------------------------*/ %let urc = %sysfunc(filename(fname,%quote(&utlfkil))); /*-------------------------------------------------*\ | Delete file if it exits | \*-------------------------------------------------*/ %if &urc = 0 and %sysfunc(fexist(&fname)) %then %let urc = %sysfunc(fdelete(&fname)); /*-------------------------------------------------*\ | Close file -- deassign file reference | \*-------------------------------------------------*/ %let urc = %sysfunc(filename(fname,'')); run; %mend utlfkil; filename pipetree pipe 'tree "h:\oto" /F /A' lrecl=5000; data a; infile pipetree truncover; input dirlist $char1000.; if index(dirlist,'.') =0; put dirlist; run; /* H:\OTO | +---chk | +---doc | +---exe | +---logisticmacros | A55770 | +---Luk | | | +---book programs | | */