BookmarkSubscribeRSS Feed
1 REPLY 1
Eva
Quartz | Level 8 Eva
Quartz | Level 8

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

Eva

 

%macro find_files (p_startdir  = );


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

	/* 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); 
		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); 
		/* 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; 
		   end; 
		   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; 
		   end; 
		   end;
		rc = dclose(did);
	run;


	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);
	run;
	proc sort data=sas_progs_found nodupkey;
	   by filename;
	run;

	%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 ;
	%end;

%mend;
 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1087 views
  • 0 likes
  • 1 in conversation