DATA Step, Macro, Functions and more

Assigning array to macro variable

Reply
Contributor
Posts: 47

Assigning array to macro variable

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

 

 

 

 

 

Respected Advisor
Posts: 2,989

Re: Assigning array to macro variable

[ Edited ]
Posted in reply to MikeFranz

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
PROC Star
Posts: 1,772

Re: Assigning array to macro variable

Posted in reply to MikeFranz

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;
Super User
Super User
Posts: 8,074

Re: Assigning array to macro variable

[ Edited ]
Posted in reply to MikeFranz

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

 

Contributor
Posts: 47

Re: Assigning array to macro variable

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?
Super User
Super User
Posts: 8,074

Re: Assigning array to macro variable

Posted in reply to MikeFranz

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

Super User
Posts: 23,683

Re: Assigning array to macro variable

Posted in reply to MikeFranz

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

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 66 views
  • 0 likes
  • 5 in conversation