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