BookmarkSubscribeRSS Feed
renjithr
Quartz | Level 8

Hi,

 

Below is  the code I got from online to archive files in the directory.

 

%macro Archive(path,duration,npath);
options mprint symbolgen;
*Filename Files pipe %unquote(%str(%') cd "&path." %str(%'));
Filename Files PIPE %unquote(%str(%') cd"&path." /b %str(%'));
*Filename Files PIPE %unquote(%str(%') dir "&path." %str(%'));

data List;
infile Files lrecl=100 truncover;
input Full_Name $100.;
dur=&duration.;
Report_Name=strip(scan(Full_Name, 1, "."));
Suffix=strip(scan(Full_Name, 3, "."));
N=length(Report_Name);
MTH_Year=strip(substr(Report_Name, N-7));
%put mth_year;
DD_MTH_Year=substr(MTH_YEAR, 7,2)||substr(MTH_YEAR, 5, 2)||substr(MTH_YEAR, 1, 4);
ddmmy=input(dd_mth_year,anydtdte8.);
%put dd_mth_year;
Age=INTCK("Month",ddmmy,today());
%put age;
run;

data _null_;
length Old_Name New_Name $100;
set List;

if Age > &Duration. then
do;
Old_Name="&path."||strip(Full_Name);
%put old_name;
New_Name="&npath."||strip(Full_Name);
%put new_name;
RC=rename(Old_Name, New_Name, 'file');
%put 'RC:' RC;

if RC=0 then
put "Note: Files moved to Archive folder: " Old_Name;
else put "Note: Files cannot be moved: " Old_Name;
end;
run;

%mend;

%archive(/apps/sas/datasets/data7/scbsr/code/team/test/vv,5,/apps/sas/datasets/data7/scbsr/code/team/test/vv1)

 

The code is not doing what I want it to do. I want to read the file name in the above directory 'peds_data_20150501' and if the duration is less than Age then archive the file.

 

Could you please tell me if I using the right commands in the filename statement in the macro?

5 REPLIES 5
LinusH
Tourmaline | Level 20
Use
options mprint symbolgen;
to see what the macro is doing.
Data never sleeps
Kurt_Bremser
Super User

First of all, your %put in the macro won't be helpful, as they will be executed before the data step is compiled and run. They will only display the variable names as text. Replace them with put statements (no %), then the data step will show you the values.

Next, do

%put %unquote(%str(%') cd "&path." %str(%'));
%put %unquote(%str(%') cd"&path." /b %str(%'));
%put %unquote(%str(%') dir "&path." %str(%'));

as a test and look if the resulting command strings make sense.

Then activate the options as @LinusH suggested, run the code and post the log from that

renjithr
Quartz | Level 8

Thank you both!

 

I added symbolgen mprint in the options and replaced %put with put and displayed report_name and suffix.

 

 

Here is my modified code and log:

 

%macro Archive(path,duration,npath);
	options mprint symbolgen;
	*Filename Files pipe %unquote(%str(%') cd "&path." %str(%'));
	Filename Files PIPE %unquote(%str(%') cd"&path." /b %str(%'));
	*Filename Files PIPE %unquote(%str(%') dir "&path." %str(%'));

	%put %unquote(%str(%') cd "&path." %str(%'));
%put %unquote(%str(%') cd"&path." /b %str(%'));
%put %unquote(%str(%') dir "&path." %str(%'));

	data List;
		infile Files lrecl=100 truncover;
		input Full_Name $100.;
		dur=&duration.;
		Report_Name=strip(scan(Full_Name, 1, "."));
		put'Report_name:' report_name;
		Suffix=strip(scan(Full_Name, 3, "."));
		put 'Suffix:' suffix;
		N=length(Report_Name);
		MTH_Year=strip(substr(Report_Name, N-7));
		DD_MTH_Year=substr(MTH_YEAR, 7,2)||substr(MTH_YEAR, 5, 2)||substr(MTH_YEAR, 1, 4);
		ddmmy=input(dd_mth_year,anydtdte8.);
		Age=INTCK("Month",ddmmy,today());
	run;

	data _null_;
		length Old_Name New_Name $100;
		set List;

		if Age > &Duration. then
			do;
				Old_Name="&path."||strip(Full_Name);
				%put old_name;
				New_Name="&npath."||strip(Full_Name);
				%put new_name;
				RC=rename(Old_Name, New_Name, 'file');
				%put 'RC:' RC;

				if RC=0 then
					put "Note: Files moved to Archive folder: " Old_Name;
				else put "Note: Files cannot be moved: " Old_Name;
			end;
	run;

%mend;

%archive(/apps/sas/datasets/data7/scbsr/code/teamr/test/vv,5,/apps/sas/datasets/data7/scbsr/code/teamr/test/vv1)

Log:

1          %_eg_hidenotesandsource;
MPRINT(_EG_HIDENOTESANDSOURCE):   options nonotes;
MPRINT(_EG_HIDENOTESANDSOURCE):   options nosource;
SYMBOLGEN:  Macro variable _EGNOTES resolves to NOTES
MPRINT(_EG_RESTORENOTESANDSOURCE):   options NOTES;
SYMBOLGEN:  Macro variable _EGSOURCE resolves to SOURCE
MPRINT(_EG_RESTORENOTESANDSOURCE):   options SOURCE;
5          %_eg_hidenotesandsource;
MPRINT(_EG_HIDENOTESANDSOURCE):   options nonotes;
MPRINT(_EG_HIDENOTESANDSOURCE):   options nosource;
SYMBOLGEN:  Macro variable SASWORKLOCATION resolves to 
            "/apps/sas/datasets/temp01/SAS_work676B01870076_nzapap68/SAS_work6D2401870076_nzapap68/"
SYMBOLGEN:  Macro variable _EGNOTES resolves to NOTES
MPRINT(_EG_RESTORENOTESANDSOURCE):   options NOTES;
SYMBOLGEN:  Macro variable _EGSOURCE resolves to SOURCE
MPRINT(_EG_RESTORENOTESANDSOURCE):   options SOURCE;
28         
29         %macro Archive(path,duration,npath);
30         	options mprint symbolgen;
31         	*Filename Files pipe %unquote(%str(%') cd "&path." %str(%'));
32         	Filename Files PIPE %unquote(%str(%') cd"&path." /b %str(%'));
33         	*Filename Files PIPE %unquote(%str(%') dir "&path." %str(%'));
34         
35         	%put %unquote(%str(%') cd "&path." %str(%'));
36         %put %unquote(%str(%') cd"&path." /b %str(%'));
37         %put %unquote(%str(%') dir "&path." %str(%'));
38         
39         	data List;
40         		infile Files lrecl=100 truncover;
41         		input Full_Name $100.;
42         		dur=&duration.;
43         		Report_Name=strip(scan(Full_Name, 1, "."));
44         		put'Report_name:' report_name;
45         		Suffix=strip(scan(Full_Name, 3, "."));
46         		put 'Suffix:' suffix;
47         		N=length(Report_Name);
48         		MTH_Year=strip(substr(Report_Name, N-7));
49         		DD_MTH_Year=substr(MTH_YEAR, 7,2)||substr(MTH_YEAR, 5, 2)||substr(MTH_YEAR, 1, 4);
50         		ddmmy=input(dd_mth_year,anydtdte8.);
51         		Age=INTCK("Month",ddmmy,today());
52         	run;
53         
54         	data _null_;
55         		length Old_Name New_Name $100;
56         		set List;
57         
58         		if Age > &Duration. then
59         			do;
60         				Old_Name="&path."||strip(Full_Name);
61         				%put old_name;
62         				New_Name="&npath."||strip(Full_Name);
63         				%put new_name;
64         				RC=rename(Old_Name, New_Name, 'file');
65         				%put 'RC:' RC;
66         
67         				if RC=0 then
68         					put "Note: Files moved to Archive folder: " Old_Name;
69         				else put "Note: Files cannot be moved: " Old_Name;
2                                                          The SAS System                          20:18 Thursday, February 16, 2017

70         			end;
71         	run;
72         
73         %mend;
74         
75         %archive(/apps/sas/datasets/data7/scbsr/code/team/test/vv,5,/apps/sas/datasets/data7/scbsr/code/team/te
75       ! st/vv1)
MPRINT(ARCHIVE):   options mprint symbolgen;
MPRINT(ARCHIVE):   *Filename Files pipe %unquote(' cd "&path." ');
SYMBOLGEN:  Macro variable PATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv
MPRINT(ARCHIVE):   Filename Files PIPE ' cd"/apps/sas/datasets/data7/scbsr/code/team/test/vv" /b ';
MPRINT(ARCHIVE):   *Filename Files PIPE %unquote(' dir "&path." ');
SYMBOLGEN:  Macro variable PATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv
' cd "/apps/sas/datasets/data7/scbsr/code/team/test/vv" '
SYMBOLGEN:  Macro variable PATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv
' cd"/apps/sas/datasets/data7/scbsr/code/team/test/vv" /b '
SYMBOLGEN:  Macro variable PATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv
' dir "/apps/sas/datasets/data7/scbsr/code/team/test/vv" '
MPRINT(ARCHIVE):   data List;
MPRINT(ARCHIVE):   infile Files lrecl=100 truncover;
MPRINT(ARCHIVE):   input Full_Name $100.;
SYMBOLGEN:  Macro variable DURATION resolves to 5
MPRINT(ARCHIVE):   dur=5;
MPRINT(ARCHIVE):   Report_Name=strip(scan(Full_Name, 1, "."));
MPRINT(ARCHIVE):   put'Report_name:' report_name;
MPRINT(ARCHIVE):   Suffix=strip(scan(Full_Name, 3, "."));
MPRINT(ARCHIVE):   put 'Suffix:' suffix;
MPRINT(ARCHIVE):   N=length(Report_Name);
MPRINT(ARCHIVE):   MTH_Year=strip(substr(Report_Name, N-7));
MPRINT(ARCHIVE):   DD_MTH_Year=substr(MTH_YEAR, 7,2)||substr(MTH_YEAR, 5, 2)||substr(MTH_YEAR, 1, 4);
MPRINT(ARCHIVE):   ddmmy=input(dd_mth_year,anydtdte8.);
MPRINT(ARCHIVE):   Age=INTCK("Month",ddmmy,today());
MPRINT(ARCHIVE):   run;

NOTE: The infile FILES is:
      Pipe command="cd"/apps/sas/datasets/data7/scbsr/code/team/test/vv" /b "

Report_name:/usr/bin/ksh: cd/apps/sas/datasets/data7/scbsr/code/team/test/vv:  not found
Suffix: 
NOTE: 1 record was read from the infile FILES.
      The minimum record length was 85.
      The maximum record length was 85.
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 75:190   
NOTE: The data set WORK.LIST has 1 observations and 9 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds
      

MPRINT(ARCHIVE):   data _null_;
MPRINT(ARCHIVE):   length Old_Name New_Name $100;
MPRINT(ARCHIVE):   set List;
SYMBOLGEN:  Macro variable DURATION resolves to 5
MPRINT(ARCHIVE):   if Age > 5 then do;
SYMBOLGEN:  Macro variable PATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv
MPRINT(ARCHIVE):   Old_Name="/apps/sas/datasets/data7/scbsr/code/team/test/vv"||strip(Full_Name);
3                                                          The SAS System                          20:18 Thursday, February 16, 2017

old_name
SYMBOLGEN:  Macro variable NPATH resolves to /apps/sas/datasets/data7/scbsr/code/team/test/vv1
MPRINT(ARCHIVE):   New_Name="/apps/sas/datasets/data7/scbsr/code/team/test/vv1"||strip(Full_Name);
new_name
MPRINT(ARCHIVE):   RC=rename(Old_Name, New_Name, 'file');
'RC:' RC
MPRINT(ARCHIVE):   if RC=0 then put "Note: Files moved to Archive folder: " Old_Name;
MPRINT(ARCHIVE):   else put "Note: Files cannot be moved: " Old_Name;
MPRINT(ARCHIVE):   end;
MPRINT(ARCHIVE):   run;

NOTE: There were 1 observations read from the data set WORK.LIST.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

Thank you!

Kurt_Bremser
Super User

The command you should use in the filename pipe is the third one (dir), but I guess you should add the /b option so you only get filenames (no headers or additional data).

I also suggest that you test each part of the macro separately (set the macro parameters with %let and execute each step) and make sure that each part works on their own.

Next run the whole macro content as is (withoput the macro definition, parameters once again set with %let) before activating the macro definition.

Running outside of a macro definition makes it easier to see which log message belongs to which step.

Kurt_Bremser
Super User

I always state that proper knowledge of the underlying operating system is a must for SAS developers. You need to have a solid grasp of the capabilities of the system and how to use them. The question which one of the commands to use would have been moot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 1840 views
  • 0 likes
  • 3 in conversation