I would like to get help to trasform this code in macro fuction:
Data my;
Set table;
Page1=scan(file1, -1, ',');
Page2=scan(file2, -1, ',');
Page3=scan(file3, -1, ',');
Run;
Thanks.
My macro is finished with error 68-185: the function file is unknown, or cannot be accessed
%macro name;
Data my;
Set table;
Array file[3] $50 file1-file3;
%do i=1 %to 3;
Page(&i)=%scan(file(&i),-1,',');
%end;
%mend;
%name;
Is there another way?
I think you can do the whole thing with ARRAYs, a macro is not needed here.
Data my;
set table;
array file $ 50 file1-file3;
array page page1-page3; /* You may want to specify a length here too */
do i=1 to 3;
page[i]=scan(file[i],-1,',');
end;
drop i;
run;
By the way, in the future, if you are having macro errors, turn on the macro debugging options by running this command
options mprint symbolgen;
and then run the macro again. The log will be easier to debug. If you still can't figure out what is wrong, then please SHOW US the log (the entire log for the macro, every single line, every single character, with nothing chopped out). Copy the log as text and paste it into the window that appears when you click on the </> icon. This is mandatory when showing us logs.
@ari3170 wrote:
I would like to get help to trasform this code in macro fuction:
Data my;
Set table;
Page1=scan(file1, -1, ',');
Page2=scan(file2, -1, ',');
Page3=scan(file3, -1, ',');
Run;Thanks.
My macro is finished with error 68-185: the function file is unknown, or cannot be accessed
%macro name;
Data my;
Set table;
Array file[3] $50 file1-file3;
%do i=1 %to 3;
Page(&i)=%scan(file(&i),-1,',');
%end;
%mend;
%name;Is there another way?
If you want to use a function with the values of data step variables then you do not use the macro function. The Macro code generates other code, not manipulates values of variables in data sets. Also do you thing this would be valid syntax:
Page(1) = scan(File(i),-1,',');
You do not define an array Page, so Page(<any thing>) is going to generate either an undefined function or array reference
error.
If you want to create/use data step variable Page1 then the syntax to use the macro variable &i would be : Page&i
But as @PaigeMiller says, an array solution would be superior.
Note: you will get more information from macro errors if you set: Options mprint; prior to running the macro. Mprint show the generated statements from the macro processor. The error messages would appear in proximity to that generated statement in the log so you could see which particular bit is the problem.
Agree with others who have said that you don't need macros for this.
But if you are learning the macro language, it's helpful to see where your code went wrong.
In this line:
Page(&i)=%scan(file(&i),-1,',');
You don't use parentheses around macro variable references. And you want to use the SCAN data step function, not the %SCAN macro function. You can try changing it to:
Page&i=scan(file&i,-1,',');
Below macro will work, but the non-macro array approach is better and clearer.
options mprint ;
%macro name();
%local i ;
data my2;
file1='foo,bar' ;
file2='monday,tuesday' ;
file3='mon,dad' ;
%do i=1 %to 3;
Page&i=scan(file&i,-1,',');
%end;
run;
%mend;
%name()
@Quentin wrote:
Page(&i)=%scan(file(&i),-1,',');
You don't use parentheses around macro variable references. And you want to use the SCAN data step function, not the %SCAN macro function. You can try changing it to:
Page&i=scan(file&i,-1,',');
I'm sure @Quentin knows this, but to extend the explanation for the benefit of @ari3170 :
The reason for this (which someone else pointed out) is that %SCAN cannot access DATA set variable values, such as the values of DATA set variable FILE1. However SCAN can access the values of a DATA step variable. Also, %SCAN can only access the values of macro variables.
If you want the macro to generate these three statements:
Page1=scan(file1, -1, ',');
Page2=scan(file2, -1, ',');
Page3=scan(file3, -1, ',');
Then tell it to just do that.
%do i=1 %to 3;
Page&i=scan(file&i, -1, ',');
%end;
But there is no need for macro code. You can use normal SAS code instead. Use arrays.
data my;
set table;
array file[3];
array page[3] $30 ;
do index=1 to dim(file);
page[index]=scan(file[index], -1, ',');
end;
drop index;
run;
Set the length specification in the array definition for PAGE to the maximum length the variables will need to hold last word from the FILE variables. Or if the PAGE1 to PAGE3 variables already exist in the input dataset TABLE then remove the length specification from the ARRAY statement.
No macro needed, only a data step DO loop:
data my;
set table;
array file{3} $50 file1-file3;
array page{3} $20 page1-page3;
do i = 1 to 3;
page{i} = scan(file{i},-1,',');
end;
run;
There are reserved keywords you shouldn't use as variable names in a macro, file is one of them.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0y43hj7lzhq1gn1r68h65wzljbt.htm
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.