I have the below code wherein it will work if path is hard coded but it's not working when macro variable is referenced (it says 0 records read). Please help me in getting the issue resolved.
/** Working fine without macro variable **/
***************************************************************************************************
%let path1 = C:\work\test\final output;
filename DIRLIST pipe 'dir "C:\work\test\final output" /B';
data dirlist ;
length buffer $256 ;
infile dirlist length=reclen ;
input buffer $varying256. reclen ;
run ;
================================================================================================
/** Result **/
1684 %let path1 = C:\work\test\final output;
1685
1686 filename DIRLIST pipe 'dir "C:\work\test\final output" /B';
1687 data dirlist ;
1688
1689 length buffer $256 ;
1690
1691 infile dirlist length=reclen ;
1692
1693 input buffer $varying256. reclen ;
1694 run ;
NOTE: The infile DIRLIST is:
Unnamed Pipe Access Device,
PROCESS=dir "C:\work\test\final output" /B,
RECFM=V,LRECL=256
NOTE: 2 records were read from the infile DIRLIST.
The minimum record length was 3.
The maximum record length was 3.
NOTE: The data set WORK.DIRLIST has 2 observations and 1 variables.
NOTE: Compressing data set WORK.DIRLIST increased size by 100.00 percent.
Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds
/** Not Working with macro variable **/
***************************************************************************************************
%let path1 = C:\work\test\final output;
filename DIRLIST pipe 'dir "&path1." /B';
data dirlist ;
length buffer $256 ;
infile dirlist length=reclen ;
input buffer $varying256. reclen ;
run ;
================================================================================================
/** Result **/
1695 %let path1 = C:\work\test\final output;
1696
1697 filename DIRLIST pipe 'dir "&path1." /B';
1698 data dirlist ;
1699
1700 length buffer $256 ;
1701
1702 infile dirlist length=reclen ;
1703
1704 input buffer $varying256. reclen ;
1705 run ;
NOTE: The infile DIRLIST is:
Unnamed Pipe Access Device,
PROCESS=dir "&path1." /B,RECFM=V,LRECL=256
Stderr output:
File Not Found
NOTE: 0 records were read from the infile DIRLIST.
NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
/** Not Working with macro variable **/
***************************************************************************************************
%let path1 = C:\work\test\final output;
filename DIRLIST pipe 'dir "&path1." /B';
data dirlist ;
length buffer $256 ;
infile dirlist length=reclen ;
input buffer $varying256. reclen ;
run ;
================================================================================================
/** Result **/
1695 %let path1 = C:\work\test\final output;
1696
1697 filename DIRLIST pipe 'dir "&path1." /B';
1698 data dirlist ;
1699
1700 length buffer $256 ;
1701
1702 infile dirlist length=reclen ;
1703
1704 input buffer $varying256. reclen ;
1705 run ;
NOTE: The infile DIRLIST is:
Unnamed Pipe Access Device,
PROCESS=dir "&path1." /B,RECFM=V,LRECL=256
Stderr output:
File Not Found
NOTE: 0 records were read from the infile DIRLIST.
NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
Double up on the double quotes:
%let path1 = C:\work\test\final output;
filename DIRLIST pipe "dir ""&path1"" /B";
I think this is documented in the Concepts Guide somewhere, but it's been a while since I've read it.
However, for this scenario, I've gotten good results from DOPEN/DREAD. In general, I prefer to use portable code when I can. Check out the sample code here: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000209687.htm. It's easily converted to a data step. The only downside is no support for wildcards in DOPEN, but this is easily supported with prxmatch.
Untested:
data dirlist;
length filename $200;
rc=filename("dir","c:\temp");
did=dopen("dir");
do i=1 to dnum(did);
filename=dread(did,i);
if prxmatch("/^test.*\.sas/io",filename) then output;
end;
did=dclose(did);
rc=filename("dir");
run;
This would keep all the files named test*.sas in c:\temp.
I admit the prxmatch syntax isn't quite as straightforward as the dir command's wildcards, but it's not hard once you get used to it. Plus you don't need it if you want to read the entire directory (your example doesn't use wildcards in the dir command).
It may look like a lot of code but it performs quite well, esp. since you're not spawning an external process to execute the pipe.
HTH,
Scott
Macro variables doesn't resolve within single quotes.
/** Not Working with macro variable **/
***************************************************************************************************
%let path1 = C:\work\test\final output;
filename DIRLIST pipe 'dir "&path1." /B';
data dirlist ;
length buffer $256 ;
infile dirlist length=reclen ;
input buffer $varying256. reclen ;
run ;
================================================================================================
/** Result **/
1695 %let path1 = C:\work\test\final output;
1696
1697 filename DIRLIST pipe 'dir "&path1." /B';
1698 data dirlist ;
1699
1700 length buffer $256 ;
1701
1702 infile dirlist length=reclen ;
1703
1704 input buffer $varying256. reclen ;
1705 run ;
NOTE: The infile DIRLIST is:
Unnamed Pipe Access Device,
PROCESS=dir "&path1." /B,RECFM=V,LRECL=256
Stderr output:
File Not Found
NOTE: 0 records were read from the infile DIRLIST.
NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
Double up on the double quotes:
%let path1 = C:\work\test\final output;
filename DIRLIST pipe "dir ""&path1"" /B";
I think this is documented in the Concepts Guide somewhere, but it's been a while since I've read it.
However, for this scenario, I've gotten good results from DOPEN/DREAD. In general, I prefer to use portable code when I can. Check out the sample code here: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000209687.htm. It's easily converted to a data step. The only downside is no support for wildcards in DOPEN, but this is easily supported with prxmatch.
Untested:
data dirlist;
length filename $200;
rc=filename("dir","c:\temp");
did=dopen("dir");
do i=1 to dnum(did);
filename=dread(did,i);
if prxmatch("/^test.*\.sas/io",filename) then output;
end;
did=dclose(did);
rc=filename("dir");
run;
This would keep all the files named test*.sas in c:\temp.
I admit the prxmatch syntax isn't quite as straightforward as the dir command's wildcards, but it's not hard once you get used to it. Plus you don't need it if you want to read the entire directory (your example doesn't use wildcards in the dir command).
It may look like a lot of code but it performs quite well, esp. since you're not spawning an external process to execute the pipe.
HTH,
Scott
Hi, Scott. Double up on the double quotes is working fine. Thanks a lot for your help
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.