DATA Step, Macro, Functions and more

Reading filenames from directory: change user for acces to other paths

Regular Contributor
Regular Contributor
Posts: 158

Reading filenames from directory: change user for acces to other paths

[ Edited ]


change user

Regular Contributor
Regular Contributor
Posts: 158

Re: Reading filenames from directory: change user for acces to other paths

[ Edited ]

Dear all,


I read filenames from a given Directory (path) which works well. Now I'd like to read the filenames from a Directory to which I have no Access, but a technical user has (I know userid and Password).


My question is: how can I use this userid in the code?, i.e. Change the user to read the filenames? Please see code below.


Best wishes



%macro find_files (p_startdir  = );

	data dirs_found (compress=no); 
		length Root $120.; 
		root = "&p_startdir."; 

	/* Updated list of directories searched */ 
	data dirs_found 
		files_found (compress=no); /* Names of files found. */
		keep Path FileName FileType;
		length fref $8 Filename $120 FileType $16;
		/* Read the name of a directory to search. */ 
		modify dirs_found;
		/* Make a copy of the name, because we might reset root. */ 
		Path = root;
		rc = filename(fref, path);
		if rc = 0 then do; 
		   did = dopen(fref); 
		   rc = filename(fref); 
		else do; 
		   length msg $200.; 
		   msg = sysmsg(); 
		   putlog msg=; 
		   did = .; 
		if did <= 0 then do; 
		   putlog 'ERR' 'OR: Unable to open ' Path=; 
		dnum = dnum(did);
		do i = 1 to dnum; 
		   filename = dread(did, i); 
		   fid = mopen(did, filename); 
		/* It's not explicitly documented, but the SAS online */ 
		/* examples show that a return value of 0 from mopen */ 
		/* means a directory name, and 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); 
		      if filename = filetype then filetype = ' '; 
		      output files_found; 
		   else do; 
		      /* A directory name was found; calculate the complete */ 
		      /* path, and add it to the dirs_found data set, */ 
		      /* where it will be read in the next iteration of this */ 
		      /* data step. */ 
		      root = catt(path, "/", filename); 
		      output dirs_found; 
		rc = dclose(did);

	data sas_progs_found (keep=filename);
	   set files_found (where=(Filetype eq "sas"));
	   filename_alt = filename;
	   laenge = length(filename_alt);
	   filename= "%" || substr(filename_alt,1, laenge-4);
	proc sort data=sas_progs_found nodupkey;
	   by filename;

	%if "&p_DebugMode." eq "J" %then %do;
	   title ****** Directories *******;
	   proc print data=dirs_found; run;
	   title ****** Files *******;
	   proc print data=files_found; run;
	   title ****** SAS-Programme *******;
	   proc print data=sas_progs_found; run;
	   title ;

Ask a Question
Discussion stats
  • 1 reply
  • 1 in conversation