BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Hello, 

 

Is there a way to eliminate the file extension.

 

%macro lastmodifiedfile2(path,string,MName);

/** Change the filename below to the following to run on UNIX **/
/** filename test pipe "ls &path -rt"; **/

/** Change the filename below to the following to run on Windows **/
/** filename test pipe "dir &path /od /t:w /b"; **/

/*filename test pipe "ls &path -rt";*/
/*filename test pipe "ls &path -rt|basename |grep &string.";*/
filename test pipe "ls &path -rt|grep &string. ";

data _null_;
infile test;
input;
call symputx("&Mname.",_infile_,'g');
run;

%put Last file to be modified in &path is: &Mname.;

%mend lastmodifiedfile2;

%lastmodifiedfile2(&soudest1.,&suffix1., FName1);

%put &Fname1. 

&Fname1 is equal to pilote_2017_2021_validation_v004.mdf.0.0.0.spds9

 

Is there an extra command I can add to get only pilote_2017_2021_validation_v004

 

Regards,

1 ACCEPTED SOLUTION

Accepted Solutions
jarapoch
Obsidian | Level 7

Have you tried SCAN function in the call symputx? 

 

Something like that:


%macro lastmodifiedfile2(path,string,MName);

/** Change the filename below to the following to run on UNIX **/
/** filename test pipe "ls &path -rt"; **/

/** Change the filename below to the following to run on Windows **/
/** filename test pipe "dir &path /od /t:w /b"; **/

/*filename test pipe "ls &path -rt";*/
/*filename test pipe "ls &path -rt|basename |grep &string.";*/
filename test pipe "ls &path -rt|grep &string. ";

data _null_;
infile test;
input;
call symputx("&Mname.",scan(_infile_,1,'.'),'g');
run;

%put Last file to be modified in &path is: &Mname.;

%mend lastmodifiedfile2;


%lastmodifiedfile2(&soudest1.,&suffix1., FName1);

View solution in original post

5 REPLIES 5
jarapoch
Obsidian | Level 7

Have you tried SCAN function in the call symputx? 

 

Something like that:


%macro lastmodifiedfile2(path,string,MName);

/** Change the filename below to the following to run on UNIX **/
/** filename test pipe "ls &path -rt"; **/

/** Change the filename below to the following to run on Windows **/
/** filename test pipe "dir &path /od /t:w /b"; **/

/*filename test pipe "ls &path -rt";*/
/*filename test pipe "ls &path -rt|basename |grep &string.";*/
filename test pipe "ls &path -rt|grep &string. ";

data _null_;
infile test;
input;
call symputx("&Mname.",scan(_infile_,1,'.'),'g');
run;

%put Last file to be modified in &path is: &Mname.;

%mend lastmodifiedfile2;


%lastmodifiedfile2(&soudest1.,&suffix1., FName1);
Kurt_Bremser
Super User

I would rather use FINDC with "B", so I get the position of the last period; filenames might contain more than one period, and only the last is wanted to remove the extension.

call symputx("&Mname.",substr(_infile_,1,findc(_infile_,".","b")-1),"g");

And to only get the first filename returned by the external command, STOP at the end of the data step, so that only one iteration is performed.

Or add another command:

ls &path -rt|grep &string. |head -1
alepage
Barite | Level 11
Hello Mr. Kurt Bremser,

I have communicate with you in the past and I know that you are very strong in SAS as in Unix. Could you please provide me a link where I can get a good PDF document to learn Unix.

Also, this command filename test pipe "ls &path -rt|grep &string. |head -1"; did not give me the expected result which should be for example: pilote_2017_2021_scenario_v003
Kurt_Bremser
Super User

ls -rt gives you the files in reverse order of the modification time. grep filters out those files containing the string in the name, and head -1 filters the first line.

Adding l to the ls call (ls -lrt) displays (among other things) the modification time.

So you could read the results of the simple ls -lrt into a dataset, and inspect that.

 

"Learning UNIX" is so broad that it dwarfs "learning Shakespeare", by orders of magnitude.

Start with the basic utilities you will use:

ls

cd

cp

mv

rm

pwd

more (or its more modern brother less)

cat

echo

grep

find

head

tail

kill

 

Next, make yourself familar with the most widely used shell, bash, and how to write scripts in it.

 

Doing a google search for "command manual" will quickly lead you to resources for the command.

 

A good starting point will also be the the manufacturer's website, like this for IBM's AIX.

Kurt_Bremser
Super User

If, OTOH, you want to extract

pilote_2017_2021_validation_v004

from

pilote_2017_2021_validation_v004.mdf.0.0.0.spds9

then the SCAN method is valid.

 

Examples like this show how hard it can be to make a macro so flexible that it deals with all possible scenarios; in the end you end up doing more work on the macro than the discrete solutions would need.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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