BookmarkSubscribeRSS Feed
MikeFranz
Quartz | Level 8

Hi,

 

I have the following code:

 

DATA _null_;
	array files [1] $20 ('property.csv' 'lease.csv');
do i = 0 to 1; length filein 8 fileid 8;
filein = fopen("/transport/default/run/incoming/files[i]",'I',1,'B');
end; RUN;

 

I am trying to loop through multiple CSVs saved on the server and copy them across to another folder (this is just part of the code). The issue, however, is that the code in filein evaluates directly to /transport/default/run/incoming/files[0]

 

How do I force it to evaluate "files[0]" to 'property.csv' and "files[1]" to 'lease.csv'?

 

Thank you!

 

Mike

 

 

 

 

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Arrays don't use index of 0.

 

You probably need

 

do i = 1  to 2;

Also, you don't want the files[i] inside the quotes, you'd need to concatenate it with the previous text. 

--
Paige Miller
novinosrin
Tourmaline | Level 20

How about this?

 

DATA _null_;
	array files [0:1] $20 ('property.csv' 'lease.csv');D
        do i = 0  to 1;
	      length filein 8 fileid 8;
              filein = fopen("/transport/default/run/incoming/files[i]",'I',1,'B');
        end;
RUN;
Tom
Super User Tom
Super User

@MikeFranz wrote:

Hi,

 

I have the following code:

 

DATA _null_;
	array files [1] $20 ('property.csv' 'lease.csv');
do i = 0 to 1; length filein 8 fileid 8;
filein = fopen("/transport/default/run/incoming/files[i]",'I',1,'B');
end; RUN;

 

I am trying to loop through multiple CSVs saved on the server and copy them across to another folder (this is just part of the code). The issue, however, is that the code in filein evaluates directly to /transport/default/run/incoming/files[0]

 

How do I force it to evaluate "files[0]" to 'property.csv' and "files[1]" to 'lease.csv'?

 

Thank you!

 

Mike 


SAS is written to match how humans think so by default arrays index from 1 and not zero.  You can specify the lower and upper bounds of an array dimensions index if you really want.

 

But for this problem it is easier to just tell the DO loop itself what values of FILENAME you want to loop over and skip the array and indexing.

length filename path fullname $200 ;
path='/transport/default/run/incoming';
do filename = 'property.csv','lease.csv';
  fullname = catx('/',path,filename);
  ...
end;

 

MikeFranz
Quartz | Level 8
Thanks for the above. I keep, however, getting an note saying "In a call to the FOPEN routine, the fileref /vya/transport/default/vyarun/incoming1/ah_property.csv exceeds 8 characters, and
will be truncated."
Any idea why that may be?
Tom
Super User Tom
Super User

@MikeFranz wrote:
Thanks for the above. I keep, however, getting an note saying "In a call to the FOPEN routine, the fileref /vya/transport/default/vyarun/incoming1/ah_property.csv exceeds 8 characters, and
will be truncated."
Any idea why that may be?

Read the manual. There are actually many examples.

 

The FOPEN() function wants a fileref and nor a physical filename.  Look at the FILENAME() function if you want to define the fileref in your data step.

Reeza
Super User

You can’t just put arrayName[i] in the string like that. 

You can use CATX though to create the string. 

You can also use FCOPY() to move files around. 

 

Path1 = catx(“\”, path, filename[1]);

 

RC = FCOPY(path1, path2);

 

 

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
  • 972 views
  • 0 likes
  • 5 in conversation