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
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.
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;
@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 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.
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);
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.
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.