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

Hello Community, 

In the past I could import all .txt format files name into a dataset using the below code. 

filename have pipe 'dir /b /s "myfolder\*.txt" ';
data want;
   infile have truncover;
   input filename $300.;
run;

 I wonder if there is a similar technique for listing  .png files.

Actually, I have around 200 .png files that I need to import into rtf file. Each file ends with "eCDF.png" or "ePDF.png". Without reading all files name using SAS, I will need to type in every single time which is very time consuming. 
Appreciate your guidance and tips!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@A_Kh wrote:

Hi @quickbluefish , thank you for the feedback!
I believe eliminating the quote around the path also causes an issue, and I'm aware of the double quote standard for macro variables.  I still think that "dir command" doesn't read macro variable as SAS does.  


If the outermost quotes are double quotes then the macro processor will resolve any macro variable within the quoted string BEFORE any other commands execute. The dir command will "see" the path with the resolved macro variable.

 

The challenge is that if your windows path contains blanks then the path itself needs also to be in double quotes for the dir command (and not single quotes). 

I can't test it but at least one of below two options should work.

/* option 1 */
filename myfile pipe %bquote(dir /b /s "C:\&user\myfolder\*.png");

/* option 2 */
filename myfile pipe "dir /b /s ""C:\&user\myfolder\*.png""";

And then there is of course also the option to use SAS functions only for the directory listing which has the advantage that the same code will work for Windows and Unix environments and also with NOXCMD.

 


 

View solution in original post

13 REPLIES 13
Quentin
Super User

Can you post the code you are using to import one .png to an rtf file?  

 

If you have that code working, then there are plenty of macros that can give you a list of all .png files in a directory.  e.g.:

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0js70lrkxo6uvn1fl4a5aafnlgt.htm

 

So if you have working code for one .png file, you could use a macro to loop over a list of .png files.

 

A_Kh
Barite | Level 11

Hi @Quentin ,  thank you for your input!
Sure. The below is the way I'm importing images into rtf file. I would use macro loop once I figure out to list all files in the study folder/subfolders... 

ods escapechar='^';
ods options nodate orientation=landscape; 
ods rtf file="C:\Desktop\test rtf.rtf" contents=yes toc_data ;

	ods rtf text='^S={outputwidth=100%  postimage="C:\myfolder\myfile-ecdf.png" just=c}  {\tc\f3\fs0\cf8 Table 1. myfile - eCDF}';

	ods rtf text='^S={outputwidth=100%  postimage="C:\myfolder\myfile-epdf.png" just=c}  {\tc\f3\fs0\cf8 Table 2.myfile - ePDF}';

ods rtf close; 

I had also checked the source you referred and didn't see any info about .png extension.

quickbluefish
Barite | Level 11
Did you try changing *.txt to *.png?
A_Kh
Barite | Level 11

Actually, I figured it out. 
Looks like the windows system doesn't resolve macro variable when used in FILENAME with PIPE. 
I replaced the macro variable &user with username and the code worked.  My apologies for hiding that crucial part of the problem in my first post. 

filename have pipe 'dir /b /s "C/&user/myfolder\*.png" ';
data want;
   infile have truncover;
   input filename $300.;
run;


filename have pipe 'dir /b /s "C/username/myfolder\*.png" ';
data want;
   infile have truncover;
   input filename $300.;
run;



quickbluefish
Barite | Level 11

Great.  The only reason it's not resolving the macro name is because you have the whole string (starting with dir) inside single quotes.  SAS will only resolve macro variables in double quotes (or no quotes at all).  You'd have to do something a little trickier to get that particular thing to work with a macro variable, but probably just easier to hardcode your user name unless this really needs to be dynamic.

It might work just like this (assuming no spaces in your file path):

filename have pipe "dir /b /s C/&user/myfolder\*.png";
A_Kh
Barite | Level 11

Hi @quickbluefish , thank you for the feedback!
I believe eliminating the quote around the path also causes an issue, and I'm aware of the double quote standard for macro variables.  I still think that "dir command" doesn't read macro variable as SAS does.  

Patrick
Opal | Level 21

@A_Kh wrote:

Hi @quickbluefish , thank you for the feedback!
I believe eliminating the quote around the path also causes an issue, and I'm aware of the double quote standard for macro variables.  I still think that "dir command" doesn't read macro variable as SAS does.  


If the outermost quotes are double quotes then the macro processor will resolve any macro variable within the quoted string BEFORE any other commands execute. The dir command will "see" the path with the resolved macro variable.

 

The challenge is that if your windows path contains blanks then the path itself needs also to be in double quotes for the dir command (and not single quotes). 

I can't test it but at least one of below two options should work.

/* option 1 */
filename myfile pipe %bquote(dir /b /s "C:\&user\myfolder\*.png");

/* option 2 */
filename myfile pipe "dir /b /s ""C:\&user\myfolder\*.png""";

And then there is of course also the option to use SAS functions only for the directory listing which has the advantage that the same code will work for Windows and Unix environments and also with NOXCMD.

 


 

Ksharp
Super User

Also could try this:

filename myfile pipe %sysfunc(quote(dir /b /s "C:\&user\myfolder\*.png"));
A_Kh
Barite | Level 11

This worked perfectly!

Thank you again!

A_Kh
Barite | Level 11

Hi @Patrick , 

Thank you for guidance!
Yes, I've multiple blanks in the path, and the path is long enough. The second option worked for me. I've not seen before that spaces in the pathname cause a problem. I use the same path in my everyday work, looks like when path is nested in another pair of quotes, spaces make difference. 


Ksharp
Super User

Here is an example:

%let path= c:\temp ;  /*the pathname to store the PNG files*/

data fname;
rc=filename('x',"&path");
did=dopen('x');
do i=1 to dnum(did);
 fname=dread(did,i);
 if upcase(scan(fname,-1,'.'))='PNG' then output;  /*Only output .png files*/
end;
did=dclose(did);
run;



%macro print_png(fname=,title=);
title j=l "&title.";
proc report data=x nowd noheader style={outputwidth=100% } ;
column x ;
define x/display;
compute x;
call define('x','style','style={ preimage="&path.\&fname." bordertopcolor=white borderbottomcolor=white borderrightcolor=white borderleftcolor=white}');
endcomp;
run;
%mend;

ods _all_ close;
ods rtf file="&path.\want.rtf" style=minimal dpi=300 startpage=no bodytitle;
data x;
x=' ';output;
run;
title;
data _null_;
set fname;
call execute(catt('%nrstr(%print_png)(fname=',fname,',title=', catx(' ','Table',_n_) ,')'));
run;
ods rtf close;

 

Ksharp_0-1740708059099.png

 

A_Kh
Barite | Level 11

Hi @Ksharp , 

Thank you for tips! 
This also worked, but looks like it reads only one folder. Is there a workaround of this way to read all subfolders as well? 

Ksharp
Super User
I remember @Tom has such a marco to read all subfolders.
You also could use command "dir *.png /s /b" as you showd to get job done.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 13 replies
  • 2809 views
  • 4 likes
  • 5 in conversation