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

Hello! 

 

For reporting purposes, I am trying to get a list of files from a directory as a variable in a sas dataset. I am using the following code to access a test directory (unfortunately I can't show the actual filenames in the actual directory, so below has a lot of dummy code/placeholders):

 

 

filename pipedir pipe "dir test/users/documents" lrecl=5000;  

data test;
	infile pipedir truncover; 
    input line $char1000.;
    put line;
run;

However, instead of one filename per obs, the variable line in work.test has 4 filenames per obs: 

 

yawenyu929_0-1621492741131.png

Here's how work.test should look:

yawenyu929_1-1621492772240.png

 

I tried 'dlm=".doc"' and 'dlm=" "', but I was not able to fix it. Does anyone have any suggestions?

 

Thank you in advance. 

 

Best,

Yawen 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What operating system is your SAS code running on?  Check the macro variables SYSSCP and SYSSCPL.

 

Your output looks like the output of ls on unix to a terminal. 

 

But if you are using Unix why did you use dir instead of ls as the command? 

 

Also when piped to another process ls will automatically generate only one name per line.

 

Try using ls instead of dir as the command.

View solution in original post

6 REPLIES 6
Oligolas
Barite | Level 11

Hi,

add the /b parameter and enclose your path in double quotes like in

filename pipedir pipe "dir ""C:\temp\*.*"" /b" lrecl=5000;  
________________________

- Cheers -

Kurt_Bremser
Super User

Use SAS on-board tools:

data filenames;
length
  fref $8
  fname $200
;
rc = filename(fref,"test/users/documents");
did = dopen(fref);
if did ne 0
then do;
  do i = 1 to dnum(did);
    fname = dread(did,i);
    output;
  end;
  rc = dclose(did);
end;
rc = filename(fref,"");
keep fname;
run;
Tom
Super User Tom
Super User

What operating system is your SAS code running on?  Check the macro variables SYSSCP and SYSSCPL.

 

Your output looks like the output of ls on unix to a terminal. 

 

But if you are using Unix why did you use dir instead of ls as the command? 

 

Also when piped to another process ls will automatically generate only one name per line.

 

Try using ls instead of dir as the command.

yawenyu929
Fluorite | Level 6

It's amazing that you realized I was programming in a unix environment. Thank you so much!

Tom
Super User Tom
Super User

@Kurt_Bremser wrote:

I guess they set up an alias for the ls command called dir, for the people coming over from Windows.


It must be something other than an alias.  The ls command knows when it is being piped and adjusts how it displays multiple names.

 

On my Red Hat Linux server someone has installed a dir command that appears to be from the GNU library.  It seems to behave like the symptom in this question.

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
  • 6 replies
  • 654 views
  • 0 likes
  • 4 in conversation