Hi The original code is below and I tried to make these code to a macro:
data work.split_data;
array a[2] _temporary_ (0.5,0.5);
array b[2] $ _temporary_ ('train','validation')
;
set combine_data;
run;
The macro I made is below and I keep getting errors. Can anyone help?
%macro data_split(dataset,probs,labels);
data work.split_data;
array a[2] _temporary_ &prob;
array b[2] $ _temporary_ &labels;
set &dataset;
run;
%mend;
%data_split(work.combine_data,probs=(0.5, 0.5),labels=("a", "b"));
Thank you!
When you're asking a question about an error message, please include the error message in your question.
Assuming you get the error I get:
1 data work.split_data; array a[2] _temporary_ &prob; array b[2] $ _temporary_ &labels; set &dataset; run; - 22 200 WARNING: Apparent symbolic reference PROB not resolved. ERROR 22-322: Syntax error, expecting one of the following: (, ;. ERROR 200-322: The symbol is not recognized and will be ignored.
Then the problem is a typo in your code. Your array statement references a macro variable named PROB but it should reference PROBS:
array a[2] _temporary_ &prob; *Missing an S on &probS;
When you're asking a question about an error message, please include the error message in your question.
Assuming you get the error I get:
1 data work.split_data; array a[2] _temporary_ &prob; array b[2] $ _temporary_ &labels; set &dataset; run; - 22 200 WARNING: Apparent symbolic reference PROB not resolved. ERROR 22-322: Syntax error, expecting one of the following: (, ;. ERROR 200-322: The symbol is not recognized and will be ignored.
Then the problem is a typo in your code. Your array statement references a macro variable named PROB but it should reference PROBS:
array a[2] _temporary_ &prob; *Missing an S on &probS;
Thanks, I made a really stupid mistake. ....
Assuming by "brackets" you mean normal parentheses then they are actually HELPFUL when passing values into macro parameters because they allow you to include commas in the values without the macro processor interpreting the commas as delimiters between parameter values.
So your code works fine (once you fix the typo in the macro variable name and adjust the LENGTH of the character values stored in the second array).
array a[2] _temporary_ &probs;
array b[2] $255 _temporary_ &labels;
Another useful thing to understand is that SAS does NOT need the commas in those lists of values, spaces work fine.
array a[2] _temporary_ (0.5 0.5);
array b[2] $200 _temporary_ ('train' 'validation');
The same thing applies to lists of values in other places, like the IN operator.
So you could remove the parentheses from your macro call and instead have the macro insert them. Then call the macro using spaces between the values in the lists.
%macro data_split(dataset,probs,labels);
data split_data;
array a[2] _temporary_ (&probs);
array b[2] $255 _temporary_ (&labels);
set &dataset;
* some code that actually uses A and B ;
run;
%mend;
%data_split(work.combine_data,probs=0.5 0.5,labels="a" "b")
You can include the parentheses in the macro call still since the ARRAY statement does not mind if you add extra nested pairs. Example:
67 data test; 68 array x[4] ((1 2) (3 4)); 69 put _all_; 70 run; x1=1 x2=2 x3=3 x4=4 _ERROR_=0 _N_=1
Thanks, I made really stupid mistake.
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.
Ready to level-up your skills? Choose your own adventure.