BookmarkSubscribeRSS Feed
chandan_mishra
Obsidian | Level 7

Hello

 

I am trying to rename some files in the source folder.

For example, the raw filename in the source folder is HCP_Address_20171006030056_2803. I want it to rename it to HCP_Address in the source folder where it is kept using SAS.

 

For that, I read  in the source folder:

 

 

%LET DT = %SYSFUNC(TODAY(),YYMMDDN8.);

%let source=G:\Mishrch1\SFTP\&DT.\field;
data source ; infile "dir /b ""&source\"" " pipe truncover; input fname $256. ;
IF INDEX(FNAME,'HCP_Address') then FNAME = "HCP_Address"; run;

Now I want to rename the files over here in the source folder. I am using the index function to search for the keyword. But this changes the name of the file in the source data set and not in the source folder. Please let me know how I can change the name.

 

 

Thanks

Chandan Mishra

5 REPLIES 5
Reeza
Super User

Are you working on Base SAS? Do you have X command?

chandan_mishra
Obsidian | Level 7

 Hello @Reeza Yes, I am using base SAS and I do have X command.

chandan_mishra
Obsidian | Level 7

Hello @Reeza

 

If I am using the rename function, 

%let source=G:\Mishrch1\SFTP\&DT.\field;


data _null_;
RC3=rename("&source.\HCP_Address_20171006030058_2870.txt", "&source.\HCP_Address.txt", "file");
run;


This code renames the file. But the problem I am facing the raw file name will change every day. Hence I have to change the source file name every day which I don't want to as I want to automate it. Please let me know.

 

Thanks

Chandan Mishra 

ballardw
Super User

Is the purpose of this rename so that you can use a static filename statement like:

filename myfile "&source.\HCP_Address.txt"?

 

Since your base file has a part of the name that looks a lot like a date: 20171006

then perhaps you could search the forum for one of the many threads about reading files with dates in the names, such as yesterday, last week, last month.

Then use some of that logic to read the desired file.

 

OR use the X command with DIR (or ls for Unix or OS appropriate command to list files), get the write date or last modified date and select that file.

Reeza
Super User

To me this falls under the idea of use the right tool for the job, in this case a good option is to find the windows command for renaming file and use an X command to pass that to windows. 

 

But SAS also has a RENAME function, and it has examples of renaming files:

 

http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p14axci3mo3egan1okbcydvbt433.htm&...

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
  • 5 replies
  • 7906 views
  • 2 likes
  • 3 in conversation