I have done the following dummy case where I input a condition in a specific dataset as following:
%macro filterDataOneCondition(name, dataset, condition); proc sql; CREATE TABLE &name AS SELECT * FROM %sysfunc(dequote(&dataset )) where make = %sysfunc(dequote(&condition)); quit; %mend; %filterDataOneCondition(name = table1, dataset = sashelp.cars, condition= "'Acura'"); run;
Now what I want to get is
(1) indexing different names in an array and loop to get the same answer as before but for all variables in the array
(2) is there a function to know the length of an array instead of specifying it by hand ?
Conditions: no usage of iml neither rlang.
My trial is as following:
%let Make1= "'Acura'";
%let Make2= "'Acura'";
%let Make3= "'Acura'";
%let name1= 1;
%let name2= 2;
%let name3= 3;
%let ARRAYMake= array(&Make1 &Make2 &Make3);
%let ARRAYNAME= array(&name1 &name2 &name3);
%let lenarray = 3; * How to get a function that gives me len(ARRAYMake)?;
DO i = 1 to &lenarray;
%filterDataOneCondition(name = ARRAYMake{i}, dataset = sashelp.cars, condition= ARRAYNAME{i});
run;
end;EDIT:
As a summary, what I am trying to achieve with this post is:
(1) Understanding how to create an array of variables from some that are already existing. That is why I am trying the thing about:
% let Make1 = Audi; % let Make2 = BMW; %let Arraymake = Make1 Make2 * This is what I wanted :)
(2) Understanding how to define directly the length of a vector or an array (i.e. analogy of the function length() in R or len() Python)
/*Calculating the lenght of an array*/ %let lenarray = %sysfunc(countw(&arraymake)); * This what I wanted;
(3) What I am still trying is to avoid writing the code below and instead passing a loop to the list of variables:
How to iterate a same procudure to create directly different outputs instead of having to write
%filterDataOneCondition(name = Make1, dataset = sashelp.cars, condition= name1); run; %filterDataOneCondition(name = Make2, dataset = sashelp.cars, condition= name2); run; %filterDataOneCondition(name = Make3, dataset = sashelp.cars, condition= name3); run;
but in a do loop
Thank you for the help and the time spent in advanced
There is no such thing as an ARRAY in macro code.
What exactly are you tying to do?
If you want to loop over a list of values in macro code an easy way is to put the list into a macro variable. (Works for lists that can be stored in less than 64K bytes)
%let makes=Acura|Audi|BMW;
%do i=1 %to %sysfunc(countw(&makes,|));
%let make=%scan(&makes,&i,|);
%filterDataOneCondition(name = &make, dataset = sashelp.cars, condition=(make="&make"));
%end;
There is no such thing as an ARRAY in macro code.
What exactly are you tying to do?
If you want to loop over a list of values in macro code an easy way is to put the list into a macro variable. (Works for lists that can be stored in less than 64K bytes)
%let makes=Acura|Audi|BMW;
%do i=1 %to %sysfunc(countw(&makes,|));
%let make=%scan(&makes,&i,|);
%filterDataOneCondition(name = &make, dataset = sashelp.cars, condition=(make="&make"));
%end;
What does the non-macro code this step is supposed to generate look like?
If you are attempting to call that macro %filterDataOneCondition inside a data step that will fail because the Proc SQL statement will be treated as a data step boundary.
You are currently passing a value intended to be used as a data set name but the value name = array(&Make1 &Make2 &Make3) will not be acceptable as a data set because of spaces, ( and ) characters.
In the macro language you count the number of space delimited items using
%sysfunc(countw(¯ovarname))
Example:
%let Make1= "'Acura'"; %let Make2= "'Acura'"; %let Make3= "'Acura'"; %let name1= 1; %let name2= 2; %let name3= 3; %let ARRAYMake= array(&Make1 &Make2 &Make3); %put There are %sysfunc(countw(&arraymake)) "words" in the list.;
You can look up the syntax for the COUNTW function to see where the 4 comes from n the count.
I have a strong suspicion that what ever you are attempting with the multiple quotes in the make variables is not going to work as intended either. Quote characters are not acceptable in data set names.
Thank you for the countw function ! :); What I wanted is similar to what the other person replied, but thank you for your time.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.