SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Phil20
Obsidian | Level 7

Hello my expertise, in my shop, I have a requirement to copy about 600 mainframe flat files to another one with system date at end.

 

Eg:

 

FILE.ABCD.PDS   

TO 

FILE.ABCD.PDS.D022020  (DMMDDYY)

 

I tried REXX. bit giving desired results. I cannot hard code the date because job runs thru ca7 scheduler.

 

Is this possible in SAS?

Please advise 

11 REPLIES 11
Phil20
Obsidian | Level 7

Hi Reeza

I looked the fcopy function.

 

filename src 'source.txt';
filename dest 'destination.txt';

In my case the destination file name is destination.system-date

how do I include the system date to the end of the destination file name dynamically?

Reeza
Super User
You can build the file name as a string in the data step using regular SAS string/character functions and then pass to the FILENAME function.

ext = catt(put(today, yymmddn8.), ".txt");
file_name_date = tranwrd(old_file_name, '.txt', ext);

This would replace .txt with the extension 20200220.txt

SASKiwi
PROC Star
%let datestamp = D%sysfunc(today(),ddmmyy6.);
%put datestamp = &datestamp;

filename dest "destination&datestamp..txt";
Reeza
Super User
That works but it's easier within a data step using the FILENAME() if you need to declare 600+ filerefs
Patrick
Opal | Level 21

If you've got an already working REXX then another option is to use SAS and generate this REXX with the filename/date you need, write the code to a file and then execute it using the internal reader (intrd).

Phil20
Obsidian | Level 7

REXX not working when I run it thru CA7 scheduler

Reeza
Super User
%let path = /home/fkhurshed/Demo1;
%let new_path = /home/fkhurshed/Demo1/Sample;


*get listing of all files;
%*Creates a list of all files in the DIR directory with the specified extension (EXT);
%macro list_files(dir,ext);
	%local filrf rc did memcnt name i;
	%let rc=%sysfunc(filename(filrf,&dir));
	%let did=%sysfunc(dopen(&filrf));

	%if &did eq 0 %then
		%do;
			%put Directory &dir cannot be open or does not exist;

			%return;
		%end;

	%do i = 1 %to %sysfunc(dnum(&did));
		%let name=%qsysfunc(dread(&did,&i));

		%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then
			%do;
				%put &dir\&name;
				%let file_name =  %qscan(&name,1,.);
				%put &file_name;

				data _tmp;
					length dir $512 name $100;
					dir=symget("dir");
					name=symget("name");
					path = catx('/',dir,name);
					the_name = substr(name,1,find(name,'.')-1);
				run;

				proc append base=list data=_tmp force;
				run;

				quit;

				proc sql;
					drop table _tmp;
				quit;

			%end;
		%else %if %qscan(&name,2,.) = %then
			%do;
				%list_files(&dir/&name,&ext)
			%end;
	%end;

	%let rc=%sysfunc(dclose(&did));
	%let rc=%sysfunc(filename(filrf));
%mend list_files;

%list_files(&path, csv);

options msglevel=i;

data renameProcess;
set list;

*process to rename;
status_ref1 = filename('fr1', path);

*build filename with dates;
path2 = catt(dir, "/Sample/", name, "_", put(today(), yymmddn8.), ".csv");
status_ref2 = filename('fr2', path2);

*process to copy;
status_copy = fcopy('fr1', 'fr2');

   if status_copy=0 then
      put 'Copied SRC to DEST.';
   else do;
      msg=sysmsg();
      put status_copy= msg=;
   end;

run;



Feel free to replace the macro/listing program with your own steps. Basically it follows the steps I outlined.

 

  1. Get a list of files
  2. Create a fileref for each file using FILENAME() 
  3. Use FCOPY to copy files

Tested on SODA.

Phil20
Obsidian | Level 7

Hi Reeza

its giving error on my input mainframe file

 

1 *GET LISTING OF ALL FILES;
2 %*CREATES A LIST OF ALL FILES IN THE DIR DIRECTORY WITH THE SPECIFIED
3 EXTENSION (EXT);
4 %MACRO LIST_FILES(ITH78D.SASTEST.DATASETS,EXT);
ERROR: Symbolic variable name ITH78D.SASTEST.DATASETS must contain only letters,
ERROR: Symbolic variable name ITH78D.SASTEST.DATASETS must contain only letters,
ERROR: Symbolic variable name ITH78D.SASTEST.DATASETS must contain only letters,
ERROR: Invalid macro parameter name ITH78D.SASTEST.DATASETS. It should be a val

Reeza
Super User
Maybe a mainframe issue. I'm not sure how to get a list of files on the mainframe or reference filepaths for that matter. The first parameter is the path....
Patrick
Opal | Level 21

May be share your working REXX as this will define a bit better your input and desired output.

 

Not sure why the Ca7 scheduler should prohibit to execute a REXX especially when generated dynamically and then sent to the internal reader (=executes as a child process). In a time long long ago I've generated and executed this way jobs with JCL generated on-the-fly.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 3112 views
  • 2 likes
  • 4 in conversation