%let __ats_udir=~/sasmacros; %global __ats_macros; %global __ats_url; %global __ats_run; /*get directory info*/ %macro getdir ( dummy ) ; data _null_; %if &SYSSCP = WIN %then %do; window Set_Dir irow=1 rows=24 color=white #2 @10 'The default directory for SAS macros is c:\sasmacros.' #4 @10 'To accept the default directory, simply press ENTER.' #6 @10 'Otherwise, enter your directory here:' +1 mydir $40.; display Set_Dir blank; if mydir=' ' then mydir='c:\sasmacros'; call symput('__ats_macros', trim(mydir)); stop; %end; %else %do; call symput('__ats_macros', "&__ats_udir"); %end; run; %mend getdir ; /*_net_get for downloading*/ %macro _net_get(url, filname, filoc, bin=0) ; %if &sysscp = WIN %then %let myslant=\; %else %let myslant=/; %if &bin = 1 %then %do; filename foo url "&url/&filname" TERMSTR=CRLF; filename tryout "&__ats_macros&myslant&filname"; data _null_; infile foo recfm=f lrecl=256; input; file tryout recfm=f lrecl=256; put _infile_; run; %end; %else %do; filename foo url "&url/&filname" TERMSTR=CRLF; filename tryout "&__ats_macros&myslant&filname"; data _null_; infile foo length=len; input record $varying200. len; file tryout; put record $varying200. len; run; %end; %mend _net_get; /*main macro here*/ %macro set_auto(pkg, url, dataloc, replace=no) ; /*temporarily turn off the notes option off*/ %let note=%sysfunc(getoption(notes)); %let source=%sysfunc(getoption(source)); %let merror=%sysfunc(getoption(merror)); options notes=no nosource nomerror; /* This program is supposed to run only once. */ %if &__ats_run >= 1 %then %do; data _null_; %if %bquote(&SYSSCP) = %str(WIN) %then %do; window Run_Success irow=1 rows=24 color=white #2 @10 'You only have to run this program once.' #4 @10 'If you want to change your default directory for storing SAS macros,' #6 @10 'you need to exit SAS first and rerun this program with a new directory'; display Run_Success blank; stop; %end; %else %do; put "*************************************************************************"; put "NOTES: You only have to run this program once."; put "If you want to change the default directory for storing SAS macros,"; put "you need to modify the dirctory at the beginning of this program."; put "*************************************************************************"; %end; run; %goto exit; %end; %else %getdir(); /* Checking if mydir exists already */ %let filrf=somedir; %let rc = %sysfunc(filename(filrf,"&__ats_macros")); %let did = %sysfunc(dopen(&filrf)); %let rc = %sysfunc(dclose(&did)); /* Creating the directory */ %if &did = 0 %then %do; %if &sysscp = WIN %then %do; options noxwait; %end; x "mkdir &__ats_macros"; %end; /*Create a permanent data set to keep track of macros downloaded date is going to store the system time and date and they are string variables. Assume that this program is run when a session starts to avoid possible conflict of libnames. */ /*first step: checking if a record data file already exists*/ libname _ats_lib "&__ats_macros"; %let dsname=_ats_lib.records; %if %sysfunc(exist(&dsname)) %then %do; %put ***************************************************************; %put; %put WARNING: The record data file records.sas7bdat already exists; %put It has been renamed to old.sas7bdat; %put; %put ***************************************************************; %if &sysscp = WIN %then %do; options noxwait; %let fin=&__ats_macros\records.sas7bdat; %let fout=&__ats_macros\oldrec.sas7bdat; x "copy &fin &fout"; x "del &fin"; %end; %else %do; x "cp &__ats_macros/records.sas7bdat &mydir/oldrec.sas7bdat"; x "rm &__ats_macros/records.sas7bdat"; %end; %end; proc sql; create table _ats_lib.records (pkgname char(12), url char(60), location char(20), descri char(40), date char(10)); quit; /* Check to see if __ats_macros has been set to sasautos already */ %let cauto=%sysfunc(getoption(sasautos)); %let rx=%sysfunc(rxParse("&__ats_macros")); %let match=%sysfunc(rxmatch(&rx, &cauto)); %if &match > 0 %then %do; %put NOTE: You already put &__ats_macros in your sasautos.; %put; %end; /* Check if autoexec.sas exists. If it exists, do the following. For Windows, autoexec.sas will be in the sas root directory. Do nothing. For Unix, copy the autoexec.sas over to the home directory of the user. SAS will look for autoexec.sas from the current directory to home directory and last SAS root directory. 8/8/01 */ %let check= %sysfunc(getoption(autoexec)); %if ( %bquote(&check) NE AND %bquote(&sysscp) NE WIN) %then %do; x "cp &check ~/"; %let check=~/autoexec.sas; %end; %if %bquote(&check) = %then %do; %if %bquote(&sysscp) = WIN %then %let check=autoexec.sas; %else %let check=~/autoexec.sas; %end; /* To append is not enough. What if the user has set his sasautos in his autoexec.sas already. We need to append the directory &mydir to his sasautos, instead of losing his original ones. Also sasautos is not going to work with Unix if not assigned to anything*/ %let fstr=%substr(&cauto, 1, 1); %if %bquote(&fstr) =%str(%() %then %do; %let mautol=%length(&cauto); %if &mautol >=3 %then %do; %let mautol=%eval(&mautol - 2); %let mauto_path=%substr(&cauto, 2, &mautol); %let mpath=%str(%()%str("&__ats_macros")%str(, )%str(&mauto_path)%str(%)); %end; %end; %else %let mpath=%str(%()%str(%")&__ats_macros%str(%")%str(, )&cauto%str(%)); options mautosource sasautos= &mpath; %let filrf=myfile; %let rc=%sysfunc(filename(filrf,"&check")); %let fid=%sysfunc(fopen(&filrf, a)); %if &fid > 0 %then %do; %let mystring=%str( ); %let rc=%sysfunc(fput(&fid,&mystring)); %let rc=%sysfunc(fwrite(&fid)); %let mystring=%str(/)%str(*Following line is added so SAS macros will be stored); %let rc=%sysfunc(fput(&fid,&mystring)); %let rc=%sysfunc(fwrite(&fid)); %let mystring=%str(in &__ats_macros and will be called automatically.*)%str(/); %let rc=%sysfunc(fput(&fid,&mystring)); %let rc=%sysfunc(fwrite(&fid)); %let mystring=%str( ); %let rc=%sysfunc(fput(&fid,&mystring)); %let rc=%sysfunc(fwrite(&fid)); %let mystring=%nrstr(%let) __ats_macros =&__ats_macros%str(;); %let rc=%sysfunc(fput(&fid,&mystring)); %let rc=%sysfunc(fwrite(&fid)); %let mystring=options mautosource sasautos= %quote(&mpath)%str(;); %let rc=%sysfunc(fput(&fid, &mystring)); %let rc=%sysfunc(fwrite(&fid)); %let rc=%sysfunc(fclose(&fid)); %end; %else %put %sysfunc(sysmsg()); /* Done writing to autoexec.sas */ /* grap the file &pkg.pkg and identify files to copy and copy them to the right location */ %local errchk; %if &sysscp = WIN %then %let myslant=\; %else %let myslant=/; filename foo1 url "&url/&pkg..pkg" TERMSTR=CRLF; filename package "&__ats_macros&myslant&pkg..pkg"; data _null_; infile foo1 length=len; file package ; input record $varying200. len; flag=substr(record,1, 1); rest=substr(record, 3); if flag="f" then do; call symput('fil_f', scan(record, 2, ' ')); call execute('%_net_get(&url, &fil_f, &__ats_macros)' ); call symput('errchk', flag); end; if (flag="s" and "&dataloc" ~=' ' ) then do; call symput('fil_s', scan(record, 2, ' ')); call execute('%_net_get(&url, &fil_s, &dataloc, bin=1)' ); call symput('errchk', flag); end; put rest; run; /*primary checking if package has been downloaded successfully*/ %if %bquote(&errchk) = %then %do; %put ******************************************************; %put ERROR:; %put; %put The package has NOT been downloaded successfully. ; %put Possibly you don%str(%')t have the network connection. ; %put; %put ******************************************************; %goto exit; %end; %else %do; data _null_; %if %bquote(&SYSSCP) = %str(WIN) %then %do; window Run_Success irow=1 rows=24 color=white #2 @5 'The net utility package has been downloaded successfully.' #4 @5 'You can start to use the net utility now.' #6 @5 'For more information on SAS macros, please visit our web site:' #8 @5 'http://www.ats.ucla.edu/sas/macros/default.htm'; display Run_Success blank; stop; %end; %else %do; put "*************************************************************************"; put "NOTES: The net utility package has been downloaded successfully"; put "*************************************************************************"; %end; run; %end; /*We need to put checking before update the record*/ proc sql noprint; insert into &dsname set pkgname="&pkg", url="&url", location="&__ats_macros", descri=' ', date="&SYSDATE"; quit; libname _ats_lib clear; %let __ats_url=http://www.ats.ucla.edu:80/stat/sas/macros; %let __ats_run = %eval(&__ats_run+1); %exit: options ¬e &source &merror; %mend set_auto ; %set_auto(nettools, http://www.ats.ucla.edu:80/stat/sas/macros/nettools)