Hello Experts,
Do you know, please, how to assign the multiple values (C1','C2') to te macro variables ?
%macro my_analysis(liste=,nom_part=);
data base_finale_&nom_part.;
set base_finale_0;
if Trimestre="T1" and cd_code in(&liste.);
keep PM PS PL;
run;
proc sort data=base_finale_&nom_part. nodupkey;
by _all_;
run;
proc sql noprint;
select sum(PL)
into :Montant_T1_&nom_part.
from base_finale_&nom_part.;
quit;
%PUT &&Montant_T1_&nom_part.;
Data Finale_index;
Montant_T1_&nom_part.=&&Montant_T1_&nom_part.;
run;
%mend;
%my_analysis(liste='C1','C2',nom_part="PXXX");
Thank you !
Hello @SASdevAnneMarie,
Use blanks to separate the items in macro variable liste. The IN operator doesn't need commas and the macro processor doesn't like them.
Also omit the quotation marks around the value of nom_part because they are invalid characters in the name of a dataset or (macro) variable.
%my_analysis(liste='C1' 'C2',nom_part=PXXX)
You can also use parentheses around the value being passed to protect the comma.
That can work in some places. The IN operator in a data step does not care if there are extra () in the list.
1 %let name=('Alfred','Jane'); 2 data want; 3 set sashelp.class; 4 if name in (&name); 5 run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.WANT has 2 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Extra () do not cause trouble in list of initial values of ARRAY statements either.
But in SQL (and by extension a WHERE statement anywhere) it is more persnickety about it.
6 proc print data=sashelp.class; 7 where name in (&name); - 22 200 ERROR 22-322: Syntax error, expecting one of the following: ;, !, &, AND, OR, |. ERROR 200-322: The symbol is not recognized and will be ignored. NOTE: Line generated by the macro variable "NAME". 1 ('Alfred','Jane') - 22 200 ERROR: Syntax error while parsing WHERE clause. ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, a missing value, -. ERROR 200-322: The symbol is not recognized and will be ignored. 8 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE PRINT used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 9 proc sql; 10 select * from sashelp.class 11 where name in (&name); NOTE: Line generated by the macro variable "NAME". 1 ('Alfred','Jane') -------- - 79 22 76 ERROR 79-322: Expecting a SELECT. ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &, *, **, +, ',', -, /, <, <=, <>, =, >, >=, ?, AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=. ERROR 76-322: Syntax error, statement will be ignored. 12 quit; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Another thought to save typing the quotes and avoid the potential of making mistakes when you type the quotes: You can use the %QLIST macro to take your input string in &liste that does not have quotes, and create a new macro variable with single quotes around each item in &liste.
https://github.com/sasutils/macros/blob/master/qlist.sas
So, it would look like this:
filename qlist url 'https://raw.githubusercontent.com/sasutils/macros/master/qlist.sas';
%include qlist;
%macro my_analysis(liste=,nom_part=);
data base_finale_&nom_part.;
set base_finale_0;
if Trimestre="T1" and cd_code in %qlist(&liste.,comma=0);
keep PM PS PL;
run;
/* Other code */
%mend;
%my_analysis(liste=C1 C2,nom_part=PXXX)
If you have only two elements in &LISTE like C1 C2, this code really doesn't save you much typing; if the are 12 elements in &LISTE, this code becomes very helpful, a lot less typing and virtually eliminates the potential of typographical errors when you type the quotes.
NOTE: the filename statement may not work behind some corporate firewalls, in which case you then have to download the QLIST macro as a .SAS program and save it on your hard drive and then %include that file.
You could use FINDW() function to replace IN operator:
%macro my_analysis(liste=,nom_part=); data base_finale_&nom_part.; set base_finale_0; if Trimestre="T1" and findw("&liste.", strip(cd_code) ,' ','it' ); keep PM PS PL; run; proc sort data=base_finale_&nom_part. nodupkey; by _all_; run; proc sql noprint; select sum(PL) into :Montant_T1_&nom_part. from base_finale_&nom_part.; quit; %PUT &&Montant_T1_&nom_part.; Data Finale_index; Montant_T1_&nom_part.=&&Montant_T1_&nom_part.; run; %mend; %my_analysis(liste= C1 C2 ,nom_part="PXXX");
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.