%macro web_open_file(name,type);
%global _DATAOUT_NAME;
%global _DATAOUT_MIME_TYPE;
%let _DATAOUT_NAME=&name;
%let _DATAOUT_MIME_TYPE=&type;
%mend;

%macro web_open_table(table);
%global _DATAOUT_TABLE;
%if %length(&_dataout_table)=0 %then %let _DATAOUT_TABLE=&table;
%else %let _DATAOUT_TABLE=&_DATAOUT_TABLE,&table;
%mend;

%macro web_open_url(url);
%global _DATAOUT_URL;
%let _DATAOUT_URL=&url;
%mend;

%macro web_drop_table /parmbuff;
   %let num=1;
    /* flags to determine whether a PROC SQL step is needed */
    /* or even started yet                                  */
    %let stepneeded=0;
    %let stepstarted=0;
   %let dsname=%scan(&syspbuff,&num,',()');
    %do %while(&dsname ne);        
            %if %sysfunc(exist(&dsname)) %then %do;
                    %let stepneeded=1;
                    %if (&stepstarted eq 0) %then %do;
                           proc sql;
                           %let stepstarted=1;
                    %end;
                           drop table &dsname;
            %end;
            %if %sysfunc(exist(&dsname,view)) %then %do;
                    %let stepneeded=1;
                    %if (&stepstarted eq 0) %then %do;
                           proc sql;
                           %let stepstarted=1;
                    %end;
                           drop view &dsname;
            %end;
            %let num=%eval(&num+1);
            %let dsname=%scan(&syspbuff,&num,',()');
    %end;
    %if &stepstarted %then %do;
            quit;
    %end;
%mend web_drop_table;

%macro sgdesign();
%put ERROR: SGDesigner macro can not be invoked from SAS Studio.;
%mend;

/* This gets matching tables and views -  matchtype = 'table';" */
/* No need to upcase tables and libraries, because they are stored uppercase */
%macro search_tables(get_tables, searchstr, escapechar, target, match_case);
	%if (%length(&target) = 0) %then %do;
		proc sql; 
			create table work._webeditor_search_tables_found_ as 
				select libname,memname,memlabel
					from DICTIONARY.TABLES where 
                    memname like ('%' || &searchstr || '%') 
                    %if (&escapechar ne '') %then %do;
                	    escape &escapechar
                    %end;
					or 
					%if (&match_case = true) %then %do;
						memlabel
					%end; 
					%else %do;
					    upcase(memlabel)
					%end;
					like ('%' || &searchstr || '%') 
					%if (&escapechar ne '') %then %do;
                	    escape &escapechar;
                    %end;
                    %else %do;
                        ;
                    %end;
	run;
	%end; 
	%else %do;
		proc sql; 
		create table work._webeditor_search_tables_found_ as 
			select libname,memname,memlabel
				from DICTIONARY.TABLES where 
                (
					(
                      ( memname like ('%' || &searchstr || '%')
                        %if (&escapechar ne '') %then %do;
                	     escape &escapechar
                        %end;
                       ) 
                        or 
					(
					%if (&match_case = true) %then %do;
						memlabel
					%end; 
					%else %do;
					    upcase(memlabel)
					%end;
						like ('%' || &searchstr || '%')
					%if (&escapechar ne '') %then %do;
                	    escape &escapechar
                    %end;
							)
					) 
                   
                    and %str(&target)
				);

	%end;
	data work._webeditor_search_tables_found_;
		set work._webeditor_search_tables_found_;
		subtext = memlabel;
		name = cat(cat(strip(libname), '.'),memname);
		matchtype = 'table';
	run;
%mend;


/* This gets matching librefs - matchtype = 'libref';" + */
%macro search_librefs(get_librefs, searchstr, escapechar, target, match_case);
	%if (%length(&target) = 0) %then %do;
		proc sql;
			create table work._webeditor_search_librefs_found_ as
			  select distinct libname, engine from DICTIONARY.MEMBERS where 
			  %if (&match_case = true) %then %do;
					libname
			  %end;
			  %else %do;
					upcase(libname) 
			  %end;
			  like ('%' || &searchstr || '%')
			  %if (&escapechar ne '') %then %do;
                	escape &escapechar;
              %end;
              %else %do;
                    ;
              %end;
		run;
	%end;
	%else %do;
		proc sql;
			create table work._webeditor_search_librefs_found_ as
			  select distinct libname, engine
			  from DICTIONARY.MEMBERS where ((
			  %if (&match_case = true) %then %do;
					libname
			  %end;
			  %else %do;
					upcase(libname) 
			  %end;
			  like ('%' || &searchstr || '%') 
			  %if (&escapechar ne '') %then %do;
                 escape &escapechar
              %end;
              )
              and  %str(&target));
		run;
	%end;
	
	data work._webeditor_search_librefs_found_;
		set work._webeditor_search_librefs_found_;
			matchtype = 'libref';
			name = libname;
			subtext = engine;
			drop libname engine;
	run;
%mend;

%macro search_columns(searchstr, escapechar, target, match_case);
	%if (%length(&target) = 0) %then %do;
		proc sql;
			create table work._webeditor_search_columns_found_ as
				select libname,memname,name,label
					from DICTIONARY.COLUMNS where 
						%if (&match_case = true) %then %do;
							name
						%end;
						%else %do;
							upcase(name)
						%end;
					like ('%' || &searchstr || '%') 
					%if (&escapechar ne '') %then %do;
                	    escape &escapechar
                    %end;                  

						or 
					
						%if (&match_case = true) %then %do;
							label
						%end;
						%else %do;
							upcase(label) 
						%end;
					like ('%' || &searchstr || '%')
					%if (&escapechar ne '') %then %do;
                	    escape &escapechar;
                    %end;
                    %else %do;
                        ;
                    %end;

		run;
	%end;
	%else %do;
		proc sql;
			create table work._webeditor_search_columns_found_ as
				select libname,memname,name,label
					from DICTIONARY.COLUMNS where 
    	  (			
				(
                    (
					    %if (&match_case = true) %then %do;
							name
					    %end;
					    %else %do;
							upcase(name) 
					    %end;
							like ('%' || &searchstr || '%')
						
						%if (&escapechar ne '') %then %do;
	                	    escape &escapechar
	                    %end;
	                )
					or 
					(
						%if (&match_case = true) %then %do;
							label
						%end;
						%else %do;
							upcase(label) 
						%end;
							like ('%' || &searchstr || '%')
	                    %if (&escapechar ne '') %then %do;
	                	    escape &escapechar
	                    %end;
					)
				) 					                 
 					and %str(&target)
			);
		run;
	%end;	
	
	data work._webeditor_search_cols_found_;
		length name $1024 subtext $1024 matchtype $16;
		set work._webeditor_search_columns_found_;
			if (label = '') then do;
				subtext = name;
			end;
			else do;
				subtext = catt(name, ':');
				subtext = catt(subtext, label);
			end;
			matchtype = 'column';
			name = cat(cat(strip(libname), '.'),memname);
			drop libname memname label;
	run;
%mend;

%macro search_files_and_folders(get_files, get_folders, searchstr, searchpath, search_subfolders, match_case);
options cmplib = work.srchfns;

proc fcmp outlib=work.srchfns._search_;
   function omitDirectory(path $, dir $) $ 3;
   suppressDirsLocation = symget('suppressDirsLocation');
   if path = suppressDirsLocation then do;
       lowdir = lowcase(dir);
	   result = (index(lowdir, 'my music') > 0) or
				(index(lowdir, 'my pictures') > 0) or
				(index(lowdir, 'my videos') > 0) or
				(index(lowdir, 'ntuser.') > 0) or
				(index(lowdir, 'appdata') > 0) or
				(index(lowdir, 'pictures') > 0) or
				(index(lowdir, 'music') > 0) or
				(index(lowdir, 'saved games') > 0) or
				(index(lowdir, 'links') > 0) or
				(index(lowdir, 'searches') > 0) or
				(index(lowdir, 'tracing') > 0) or
				(index(lowdir, 'video') > 0) or
				(index(lowdir, 'contacts') > 0) or
				(index(lowdir, 'favorites') > 0) or
				(index(lowdir, 'recent') > 0) or
				(index(lowdir, 'sendto') > 0) or
				(index(lowdir, 'printhood') > 0) or
				(index(lowdir, 'application data') > 0) or
				(index(lowdir, '.ini') > 0) or
				(index(lowdir, 'local settings') > 0) or
				(index(lowdir, 'templates') > 0) or
				(index(lowdir, 'nethood') > 0) or
				(index(lowdir, 'start menu') > 0) or
				(index(lowdir, 'cookies') > 0) or
				(index(lowdir, 'downloads') > 0) or
                (index(lowdir, 'my documents') > 0) or
				(index(lowdir, '.') > 0) ;
	   if result = 1 then  
	     return('yes');
	   else 
	     return('no');	        
   end;
   else
      return('no');
   endsub;
run;

	data work._all_folders_found_ (compress=no);
		length Root $1024. Path $1024. nextPath $1024. Filename $1024.;
		root = &searchpath;
		nextPath= &searchpath;
	run;
	
	data work._all_folders_found_ 	 /* Updated list of directories searched */
		work._webeditor_search_files_found_ (compress=no); /* Names of files found. */
		keep Path FileName FileType;
		length fref $8 Filename $1024 FileType $16;
		
		/* Read the name of a directory to search. */
		modify work._all_folders_found_;
		/* Make a copy of the name, because we might reset root. */
		Path = nextPath;
	%if &debug=true %then %do;
		put 'path:' path=;
	%end;

		rc = filename(fref, path);
		if rc = 0 then do;
			did = dopen(fref);
			rc = filename(fref);
		end;
		else do; 
			length msg $200.; 
			msg = sysmsg();
			putlog msg=;
			did = .;
		end;
		if did <= 0 then do;
			putlog 'ERR' 'OR: Unable to open '
			Path=;
			return;
		end;
		dnum = dnum(did);
		do i = 1 to dnum;
			filename = dread(did, i);
			fid = mopen(did, filename);
	%if &debug=true %then %do;
			put 'open:' filename=;
	%end;

			/* A return value of 0 from mopen means a directory name, anything else means a file name. */
			if fid > 0 then do;

			    /* FileType is everything after the last dot. If no dot, then no extension. */
				FileType = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
				FileType = 'none';
				if filename = filetype
					then filetype = ' ';
					
				%if &match_case=true %then %do;
					position = index(filename, &searchstr);
				%end; 
				%else %do;					
					position = index(upcase(filename), &searchstr);
				%end;
				
				if position > 0 then do;
	%if &debug=true %then %do;
				    put 'gotone:' filename =;
	%end;
					output work._webeditor_search_files_found_; 
				end;
				else do;
	%if &debug=true %then %do;
				    put 'nomatch:' filename =;
	%end;
				end;
			end;
			else   /* Add complete directory path to folders data set to be read in next iteration. */
			do;
			    omitDir=omitDirectory(path, filename);
	%if &debug=true %then %do;
			    put 'omitDir: ' omitDir =;
	%end;
			    if (omitDir = 'no') then do;
					root = catt(path, "/", filename); 
					%if &search_subfolders=true %then %do;
						nextPath = catt(path, "/", filename); 
					%end; 
					%else %do;
						nextPath = "";
					%end;
					output work._all_folders_found_;
				end;
			end;		
		end;
		rc = dclose(did); 
	run;
		
	%if &get_folders=true %then %do;
		data work._webeditor_search_folders_found_;
			set work._all_folders_found_;
			if (strip(Path) = "") then delete;
				%if &match_case=true %then %do;
					position = index(filename, &searchstr); 
				%end;
				%else %do;
					position = index(upcase(filename), &searchstr); 
				%end;
				if position = 0 then do;
	%if &debug=true %then %do;
					put 'nomatch:' filename=;
	%end;
					delete; 
				end; 
				else do;
	%if &debug=true %then %do;
				%if &match_case=true %then %do;
				    put 'gotone:' filename=;
				%end;
				%else %do;
				    put 'gotone upcase:' filename=;
				%end;
	%end;
				end; 
				name = filename;
				subtext = Root;
				matchtype = 'folder'; 
		run;
	%end;
	%if &get_files=true %then %do;
		data work._webeditor_search_files_found_;
			set work._webeditor_search_files_found_;
				name = Filename;
				subtext = Path;  
				matchtype= 'file';
		run; 
	%end;
	
	proc delete data=work.srchfns;
%mend;

%macro search_ftp_shortcut(user, pass, host, port, 
                  get_files, get_folders, 
                  searchstr, searchpath, search_subfolders, match_case, suppressedDir);
	
	%if &match_case=false %then %do;
	    %let searchstr = %upcase(&searchstr);
	%end;
	/* Clean up any prior search results */
	proc delete data=work._webeditor_search_results_;	
	
	/* Declare the results dataset structure */
	data work._webeditor_search_results_;
		length name $1024 subtext $1024 matchtype $16;
		delete;
	run;  

    /* Set up starting point for this iteration of search */              
	%let next_directory = &searchpath;
	%do %while (%length(&next_directory) > 0);
		%let fileref_name = _SSRCH_;
		%let tmp_all = work._TMPALL_;
		%let tmp_files = work._TMPFIL_;
		%let tmp_folders = work._TMPFLD_;
	
	    /* Put all files and folders at location &next_directory into table &tmp_all */
		ODS LISTING;
		FILENAME &fileref_name FTP '' LIST USER = &user PASS = &pass
		    HOST = &host PORT = &port CD = &next_directory;
			DATA &tmp_all;
			
			INFILE &fileref_name DLM = ' ';
			LENGTH permission $20 filename $256; 
			INPUT permission $ links owner file_group file_size month $ day year filename &;
			dir = index(permission, 'd');
			path = &next_directory;
			drop permission links owner file_group file_size month day year;  
			PUT _INFILE_;
		RUN;
		FILENAME &fileref_name CLEAR;
		
		/* Search through folders in &tmp_all for matches */
		/* and save matches in &tmp_folders.              */
		%if &get_folders=true %then %do;
			data &tmp_folders;
				set &tmp_all;
					%if &match_case=true %then %do;
						position = index(filename, &searchstr); 
					%end;
					%else %do;
						position = index(upcase(filename), &searchstr); 
					%end;
					if dir = 0 then do;
						delete;
					end;
					if position = 0 then do;
		%if &debug=true %then %do;
						put 'nomatch:' filename=;
		%end;
						delete; 
					end; 
					else do;
		%if &debug=true %then %do;
						put 'gotone:' filename=;
		%end;
					end; 
					name = filename;
					subtext = path;
					matchtype = 'folder';
				drop position filename path; 
			run;
			proc datasets library=work nolist;
				append base=work._webeditor_search_results_ data=&tmp_folders force; 
			run;
			/* Clean up &tmp_folders after appending results to search_results */
			proc delete data=&tmp_folders; run;	
		%end;
		
		/* Search through files in &tmp_all for matches */
		/* and save matches in &tmp_files.              */
		%if &get_files=true %then %do;
			data &tmp_files; 	 
				set &tmp_all; 
					if dir=1 then do;
						delete;
					end;
					%if &match_case=true %then %do;
						position = index(filename, &searchstr); 
					%end;
					%else %do;
						position = index(upcase(filename), &searchstr); 
					%end;
		
					if position = 0 then do;
			%if &debug=true %then %do;
						put 'nomatch:' filename=;
			%end;
						delete; 
					end; 
					else do;
			%if &debug=true %then %do;
						put 'match:' filename=;
			%end;
					end; 
					name = filename;
					subtext = path;
					matchtype = 'file';	
				drop position filename path;		
			run;
			
			proc datasets library=work nolist;
				append base=work._webeditor_search_results_ data=&tmp_files force; 
			run; 
			/* Clean up &tmp_files after appending results to search_results */
			proc delete data=&tmp_files; run;
		%end;	 		

	    %let next_directory = ;
	    
	    /* If searching subfolders, put the folders that are in the */
	    /* currently being searched folder in _folders_to_search.   */
		%if &search_subfolders = true %then %do;
            /* Delete any files from &tmp_all */		
		    data &tmp_all; set &tmp_all;
		    	if dir = 0 then do;
					delete; 
				end; 
				else do;
				    /* Replace any single quotes with double single quotes to set up next_directory */			              	
				    directory = catt("'",TRANWRD(path, "'", "''"),"/",TRANWRD(filename, "'", "''"), "'");
 				end; 
		    run;
		   
			proc datasets library=work nolist;
				append base=work._folders_to_search_ data=&tmp_all force; 
			run;
			
			data _folders_to_search_; set _folders_to_search_;
			   if _n_ = 1 then do;
					CALL SYMPUT('next_directory',  directory);
					delete;
			   end;
			run;				    
		%end;
	%if &debug=true %then %do;
		%put'next directory:' &next_directory;		
	%end;
		proc delete data=&tmp_all &tmp_files &tmp_folders; run;	
	%end;
	proc delete data=_folders_to_search_; run;
	PROC SORT DATA=work._webeditor_search_results_ sortseq=linguistic;
		BY name;
	RUN; 			
%mend;

%macro search_mvs_datasets(searchstr, searchpath, get_datasets, get_members);

    /* Clean up datasets that would have been left lying around if debug=true was on during the last search */
   	proc delete data=work._all_dsns_found_ 
					work._webeditor_search_members_found_
					work._webeditor_search_dsns_found_
					work._webeditor_search_results_;

	/* The FILESYSTEM must be set to MVS for ZDSATTR to work correctly. */
	OPTIONS FILESYSTEM=MVS;  

	/* The MVS names should all be uppercase */
	%let searchstr = %upcase(&searchstr);

%if &debug=true %then %do;
    options mlogic mprint symbolgen;
%end;

	/* Get the list of all files that match the search path. */                                             
	DATA work._all_dsns_found_;                                                              
		LENGTH XDSN $100;                                                     
		LENGTH DSNAME $48;                                                    
		XDSN = ZDSLIST('CATLG',&searchpath);                      
		XNUM = ZDSNUM(XDSN);                                                        																			
		LENGTH DSNAME $48;
		LENGTH MVS_DSNAME $44;      
												  
		L=0;                                                                  
		DO I=1 TO XNUM;     
			MVS_DSNAME = ZDSIDNM(XDSN, I);                                                     
			DSNAME = 'MVS:'|| MVS_DSNAME;                                 
			OUTPUT;                                                            
			L+LENGTH(DSNAME)+1;                                                
		END;                                                               
	    L=L-1;                                                             
	    KEEP DSNAME MVS_DSNAME;                                                          
	RUN; 

	/* If getting matching dataset names then create a dataset */
	/*   and populate it with matching MVS dataset names.      */
%if &get_datasets=true %then %do;
    data work._webeditor_search_dsns_found_; 
		set work._all_dsns_found_;
		length xattr $4096;
		LENGTH XATTRN 8;
		LENGTH XATTRC $72;
		LENGTH dsorg $8;
		LENGTH subtext $58;

		position = index(MVS_DSNAME, &searchstr);
		if position > 0 then do;
			xattr = zdsattr(MVS_DSNAME, 'File'); 
			XATTRN=ZDSXATT(xattr);
			PUT;
			DO J = 1 TO XATTRN;
			   XATTRC = ZDSYATT(XATTR, J);
			   pos = index(XATTRC, 'Data Set Organization');
			   if pos > 0 then do;
				  blank_pos = index(xattrc," ");
				  dsorg = substr(xattrc,1,blank_pos);
			   end;
			END;
		
			name = DSNAME;
			subtext = '';
			matchtype='dataset';
			output; 
%if &debug=true %then %do;
			put 'match   :' MVS_DSNAME=; 
%end;
		end;
		else do;
%if &debug=true %then %do;
			put 'no match:'  MVS_DSNAME=;
%end;
		end;
    run; 
    
	/* Merge the matching MVS dataset names into the all results datasets. */
	proc datasets library=work nolist;
		append base=work._webeditor_search_results_
			   data=work._webeditor_search_dsns_found_  force; 
	run;
%end;

/* Go through the list of all datasets and get the members of each.  */
/* Put matching members in a dataset. */
%if &get_members=true %then %do;                              
	DATA work._webeditor_search_members_found_; 
	  SET work._all_dsns_found_;                                                  
		LENGTH FILEREF $8;                                                 
		LENGTH subtext $58;
		FILEREF=' ';                                                       
		RC = FILENAME(FILEREF,DSNAME);                                     
%if &debug=true %then %do;
		PUT DSNAME= FILEREF= RC=;  
%end;                                        
		DSID = DOPEN(FILEREF);                                             
		IF DSID NE 0 THEN DO;                                              
			DNUM = DNUM(DSID);                                              
                                                 
			DO I=1 TO DNUM;                                                 
				MEMNAME = DREAD(DSID,I);                                     
				position = index(MEMNAME, &searchstr);
				if position > 0 then do;
					name = MEMNAME;
					subtext = DSNAME;
					matchtype = 'member';
					output;
%if &debug=true %then %do;
					put 'member match: ' MEMNAME;
%end;      
				end;    
				else do;
%if &debug=true %then %do;
					put 'no member match: ' MEMNAME;
%end;      
				end;                             
			END;                                                         
			RC = DCLOSE(DSID);                                              
		END;                                                            
		RC = FILENAME(FILEREF);  
    RUN;

    /* Merge the matching MVS member names into the all results datasets. */
    proc datasets library=work nolist;
		append base=work._webeditor_search_results_
               data=work._webeditor_search_members_found_  force; 
	run;  
%end;
%if &debug=false %then %do;
    /* Clean up datasets no longer needed */
   	proc delete data=work._all_dsns_found_ 
					work._webeditor_search_members_found_
					work._webeditor_search_dsns_found_;  
%end;
                                                            
%mend;


%macro search_mvs_dataset(searchstr, searchpath, match_case);
	
%if &match_case=false %then %do;
	%let searchstr = %upcase(&searchstr);
%end;
	
proc delete data=work._webeditor_search_results_;
	
data work._webeditor_search_results_ (compress=no);
    keep matchtype name subtext;
    length fref $8 Filename $1024 name $1024 subtext $1024;	
    Path = &searchpath;
 
    rc = filename(fref, &searchpath);
    if rc = 0 then do;
	  did = dopen(fref);
	  rc = filename(fref);
    end;
    else do; 
		length msg $200.; 
		msg = sysmsg();
		putlog msg=;
		did = .;
	end;
	if did <= 0 then do;
		putlog 'ERR' 'OR: Unable to open '
		Path=;
		return;
	end;
	dnum = dnum(did);
	do i = 1 to dnum;
		filename = dread(did, i);
		fid = mopen(did, filename);
%if &debug=true %then %do;
			put 'open:' filename=;
%end;

	/* A return value of 0 from mopen means a directory name, anything else means a file name. */
		if fid > 0 then do;			   					
			%if &match_case=true %then %do;
				position = index(filename, &searchstr);
			%end; 
			%else %do;					
				position = index(upcase(filename), &searchstr);
			%end;
				
			if position > 0 then do;
%if &debug=true %then %do;
		   		put 'gotone:' filename =;
%end;
               name = Filename;
               subtext = Path;
               matchtype='file';

			   output work._webeditor_search_results_; 
			end;
			else do;
%if &debug=true %then %do;
			put 'nomatch:' filename =;
%end;
			end;
		end;				
	end;
	rc = dclose(did); 
run;		
		
	proc print data=work._webeditor_search_results_;
	
%mend;


%macro merge_data(get_files, get_folders, get_tables, get_librefs, get_columns,get_locale);
	data work._webeditor_search_results_;
		length name $1024 subtext $1024 matchtype $16;
	run;
	data work._webeditor_search_results_;
		set work._webeditor_search_results_;
			if (strip(name) = "") then delete;
	run;
	%if &get_files=true %then %do;
		proc datasets library=work nolist;
			append base=work._webeditor_search_results_ data=work._webeditor_search_files_found_ force; 
		run; 
	%end;
	%if &get_folders=true %then %do; 
		proc datasets library=work nolist;
			append base=work._webeditor_search_results_ data=work._webeditor_search_folders_found_  force; 
		run;
	%end;
	%if &get_librefs=true %then %do;
		proc datasets library=work nolist;
			append base=work._webeditor_search_results_ data=work._webeditor_search_librefs_found_ force;
		run;
	%end;
	
	%if &get_tables=true %then %do; 
		proc datasets library=work nolist; 
			append base=work._webeditor_search_results_ data=work._webeditor_search_tables_found_ force; 
		run;
	%end;
	%if &get_columns=true %then %do; 
		proc datasets library=work nolist; 
			append base=work._webeditor_search_results_ data=work._webeditor_search_cols_found_ force; 
		run;
	%end;

	PROC SORT DATA=work._webeditor_search_results_ sortseq=linguistic(locale=&get_locale);
		BY name;
	RUN; 
%mend;



%macro cleanup_sasstudio_search();
	proc delete data=work._all_folders_found_ 
					work._webeditor_search_cols_found_
					work._webeditor_search_columns_found_
					work._webeditor_search_tables_found_ 
					work._webeditor_search_librefs_found_  
					work._webeditor_search_folders_found_   
					work._webeditor_search_tabs_and_cols_ 
					work._webeditor_search_files_found_;  
	run; 
%mend;


%macro do_search(get_files, get_folders, 
    get_tables, get_librefs, get_columns, 
    searchstr, escapeSearchstr, escapechar, searchpath, 
    search_subfolders, debug, target, match_case, get_locale); 
	%cleanup_sasstudio_search(); 
	
	proc delete data=work._webeditor_search_results_;
	
	%if &match_case=false %then %do;
	    %let searchstr = %upcase(&searchstr);
		%let escapeSearchstr = %upcase(&escapeSearchstr);
	%end;
	
	
	%if &get_tables=true %then %do; 
		%if (&escapechar ne '') %then %do;
			%search_tables(&get_tables, &escapeSearchstr, &escapechar, &target, &match_case);
        %end;
		%else %do;
			%search_tables(&get_tables, &searchstr, &escapechar, &target, &match_case);
		%end;
	%end; 
	%if &get_columns=true %then %do; 
		%if (&escapechar ne '') %then %do;
			%search_columns(&escapeSearchstr, &escapechar, &target, &match_case);
        %end;
		%else %do;		
			%search_columns(&searchstr, &escapechar, &target, &match_case);
		%end;
	%end; 
	%if &get_librefs=true %then %do; 
		%if (&escapechar ne '') %then %do;
			%search_librefs(&get_librefs, &escapeSearchstr, &escapechar, &target, &match_case); 
        %end;
		%else %do;
			%search_librefs(&get_librefs, &searchstr, &escapechar, &target, &match_case); 
		%end;
	%end;  
	
	%if &get_files=true or &get_folders=true %then %do; 
		%search_files_and_folders(&get_files, &get_folders, &searchstr, &searchpath, &search_subfolders, &match_case); 
	%end; 
	
	%merge_data(&get_files, &get_folders, &get_tables, &get_librefs, &get_columns,&get_locale); 

	%if &debug=false %then %do;
		%cleanup_sasstudio_search();
	%end;
	%else %do;
		options mprint mlogic notes spool;  
	%end;
%mend; 

%macro web_list_entries(catalog,type);

%let typearg=;
%let type=%upcase(&type);
%if &type^=_ALL_ and &type^=_all_ %then %let typearg= entrytype=&type;

proc catalog catalog=&catalog &typearg;
contents;
title "Catalog Entries in &catalog";
run;
quit;

%mend;

%macro web_list_catalogs(library);
	%let library=%upcase(&library);
    proc sql ;
        create table work.catalogs as select memname as Catalog, memtype as 
            Type, engine as Engine from sashelp.vmember where 
            libname="&library" and memtype="CATALOG";
        run;
        quit;
        title "Catalogs in &library";

    proc print data=work.catalogs;
    run;
%mend;

%macro web_replay_grseg(catalog,entry);
proc greplay nofs igout=&catalog;
replay &entry;
run;
%mend;

%macro sasstudio_get_zos_ds_info(DSN); 

OPTIONS FILESYSTEM=MVS;
                                                                                             
DATA work._sasstudio_dataset_info_;                                                                                                                                                                                                                                    
                                                                                                              
  LENGTH XDSN $32000;                                                                                                                                                                                                                            
  XDSN=ZDSLIST('CATLG', &DSN, XVOLS, '');                                         
                                                                                                                           
  LENGTH XNUM 8;                                                                                                                
  XNUM=ZDSNUM(XDSN);                                                                                                            
  LENGTH XIDNM $4096;                                                                                                           
  LENGTH XATTR $4096;                                                                                                           
  LENGTH XATTRC $72;                                                                                                            
  LENGTH XATTRN 8;  
  LENGTH attributeType $10;
                                                                                                            
  DO I=1 to XNUM;                                                                                                               
    XIDNM=ZDSIDNM(XDSN, I);                                                                                                     
    XATTR=ZDSATTR(XIDNM, 'ALL');                                                                                                
    XATTRN=ZDSXATT(XATTR);                                                                                                      
                                                                                                                                           
    do j=1 to xattrn;                                                                                                           
     XATTRC=ZDSYATT(XATTR, j);                                                                                                  
     blank_pos = index(xattrc," ");
		   key = substr(xattrc, blank_pos,99);
		   val= substr(xattrc,1,blank_pos);
		   pos = index(key, 'FILE ATTRIBUTES');
		   if (pos > 0) then do;
		      attributeType = 'FILE';
		   end;
		   else do;
			   pos = index(key, 'VOLUME ATTRIBUTES');
			   if (pos > 0) then do;
			      attributeType = 'VOLUME';
			   end;
			   else do;
				   pos = index(key, 'MIGRATED DATA SET ATTRIBUTES');
				   if (pos > 0) then do;
				      attributeType = 'MIGRATED';
				   end;
				   else do;
				      pos = index(key, '*****');
				   end;
			   end;		   
		   end;	   
		   if (pos = 0) then do;
		      output;
		   end; 
		   keep key val attributeType;                                                                                                            
    end;                                                                                                                        
  END;                                                                                                                          
 RUN;                                                                                                                           
%mend sasstudio_get_zos_ds_info;

%macro show_zos_dataset_attributes(dsn);
   %sasstudio_get_zos_ds_info(&dsn);
   
   data work._sasstudio_dataset_info_ds_;
       set work._sasstudio_dataset_info_;
      
       if strip(val)='.' then do; 
           val='***NONE***';
       end;
     
       position = index(attributeType, 'FILE');
       if position > 0 then do;
           output;
       end;
       drop position attributeType;
   run;
   
   data work._sasstudio_dataset_info_vol_;
       set work._sasstudio_dataset_info_;
       
       if strip(val)='.' then do; 
           val='***NONE***';
       end;
       
       position = index(attributeType, 'VOLUME');
       if position > 0 then do;
           output;
       end;
       drop position attributeType;
   run;
	
	proc print data=work._sasstudio_dataset_info_ds_ noobs label;
	LABEL key='Dataset Attribute'  val='00'x;
	   title1 &dsn;
	run;
	
	proc print data=work._sasstudio_dataset_info_vol_ noobs label;
	    title1;
		LABEL key='Volume Attribute'  val='00'x;
	run;

	proc delete data=work._sasstudio_dataset_info_
	work._sasstudio_dataset_info_ds_
	work._sasstudio_dataset_info_vol_;

%mend;

/* This was taken directly from EG at:                                  */
/* http://gitgrid.unx.sas.com/cgit/vpw/EC/Framework/tree/JobManagement/Source/jobmanager/Macros/_eg_conditional_dropds.sas */
/* This macro is used by code generated for Query nodes in an EG Process flow *
/* Conditionally delete set of tables or views, if they exists          */
/* If the member does not exist, then no action is performed   */
%macro _eg_conditional_dropds /parmbuff;
	
   	%local num;
   	%local stepneeded;
   	%local stepstarted;
   	%local dsname;
	%local name;

   	%let num=1;
	/* flags to determine whether a PROC SQL step is needed */
	/* or even started yet                                  */
	%let stepneeded=0;
	%let stepstarted=0;
   	%let dsname= %qscan(&syspbuff,&num,',()');
	%do %while(&dsname ne);	
		%let name = %sysfunc(left(&dsname));
		%if %qsysfunc(exist(&name)) %then %do;
			%let stepneeded=1;
			%if (&stepstarted eq 0) %then %do;
				proc sql;
				%let stepstarted=1;

			%end;
				drop table &name;
		%end;

		%if %sysfunc(exist(&name,view)) %then %do;
			%let stepneeded=1;
			%if (&stepstarted eq 0) %then %do;
				proc sql;
				%let stepstarted=1;
			%end;
				drop view &name;
		%end;
		%let num=%eval(&num+1);
      	%let dsname=%qscan(&syspbuff,&num,',()');
	%end;
	%if &stepstarted %then %do;
		quit;
	%end;
%mend _eg_conditional_dropds;

/* Given a fileref and a memname and memtype, we attempt to open the
member of the directory (catalog or file system directory). We
set &member_found to 1 if it can be opened, 0 if not. */
%macro _entry_exists(fileref,memname,memtype);
%global _macro_found;
%let _macro_found = 0;
data _null_;
*-----open the directory and proceed if it can be opened-----*;
handle = dopen("&fileref.");
if handle ne 0;
*-----open the member and set the macro variable based on result-----*;
mem_handle = mopen(handle,"&memname..&memtype.",'i');
call symputx('_macro_found',mem_handle ne 0);
*-----close the member if it were opened successfully-----*;
if mem_handle then rc = fclose(mem_handle);
*-----close the directory-----*;
rc = dclose(handle);
run;
%mend _entry_exists;

/* Given a macro name, we determine if it has already been
compiled. We first look in work.sasmacr, then in the sasmacr
referenced by sasmstore (if given) and then in work.sasmacX. */
%macro _compiled_macro_exists(macro_name);
options nonotes;
%global _macro_found;
*-----try work.sasmacr first to see if the compiled macro is there-----*;
filename maclib catalog "work.sasmacr";
%_entry_exists(maclib,&macro_name.,macro);
filename maclib clear;
%if &_macro_found %then %goto done;
*-----try sasmacr referenced by sasmstore if it were specified-----*;
%let sasmstore_option = %sysfunc(getoption(sasmstore));
%if %sysfunc(getoption(mstored))=MSTORED and %length(&sasmstore_option) > 0 %then %do;
filename maclib catalog "&sasmstore_option..sasmacr";
%_entry_exists(maclib,&macro_name.,macro);
%end;

%do i=1 %to 9;
%if &_macro_found %then %goto done;
filename maclib catalog "work.sasmac&i."; 
%_entry_exists(maclib,&macro_name.,macro);
filename maclib clear; 
%end;

%done: options notes;
%mend _compiled_macro_exists;

%macro studio_cas_start;

%global _macro_found;
%global syscasinit;

%let syscasinit=0;

%_compiled_macro_exists(studio_cas_init);

%if &_macro_found %then %do;

%let syscasinit=1;

%studio_cas_init;

%end;
%mend studio_cas_start;





