Hello everyone,
(Sorry for my english)
I'm trying to create an array but using an extra condition if we are in the first loop of the "DO".
You can see here my code, I think I'm not very far from succeeding but I have a syntax problem in the macro language.
%MACRO traitement_spe_idf ;
%DO i = 1 %TO 2 ;
data tempoidf&i;
set mdv.ev&i._finale;
if niveau_plus_grd = "17515";
%let nbvareer = %eval(&nbvar_eer. +2);
%let nbvarev&i. = %eval(&&nbvar_ev&i. +2);
ARRAY teer[&nbvareer.] &list_eer_finale. %IF i = 1 %THEN ceidfeer_1 ceidfeer_2 %str(;) ;
ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ %str(;) ;
do j=1 to dim(teer);
teer[j]=tev&i.[j];
end;
%IF i = 1 %THEN %DO ;
eer_q7=ceidfev1_2;
eer_q6_i1_=.;
eer_q6_i2_=.;
%END ;
%ELSE %DO ;
eer_q7=ceidfev2_1;
ceidfeer_1=ceidfev2_2;
eer_q6_i1=.;
eer_q6_i2=.;
ceidfeer_2=.;
%END ;
drop j &&list_ev&i._finale. &&list_ev&i._a_suppr.;
run;
%END ;
%MEND traitement_spe_idf ;
My problem on this code :
When i'm generating the both arrays, i want to add 2 variables if I am on the first loop (i = 1) so i tried the %IF ....
I don't know if you have any ideas.
Thank you a lot,
Onizuka
I don't see where you are getting different results for &nbvareer or &&nbvarev&i depending on whether or not &I is 1 or 2.
You could use
ARRAY teer &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2; ;
ARRAY tev&i. &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_; ;
in which case SAS figures out how many elements are in each array
Where do you set all those macro variables you are using, like
&nbvar_eer
?
When you refer to a macro variable like &I, you have to have &i in the %IF statement. Your code is missing the &. This is what it should look like:
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2 %str(;) ;
ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ %str(;) ;
Okey, i'am going to try it.
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2 %str(;) ;
ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ %str(;) ;
I think I have another problem that I will try to solve :
&nbvareer. contain the number of variable in &list_eer_finale. These macro variable were creating previously, i'm doing the code step by step, when all works, i'm putting the entire code on the %macro %mend
&&nbvarev&i. contains :
So i think that i have a problem, because when i'm doing the second loop, i don't have the additionnal two variables so it's wrong. i am going to modify this point.
The other problem in your code
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2 %str(;) ;
ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ %str(;) ;
When &i=2, then the %IF statements don't execute, and I don't think your ARRAY statements end with a semi-colon.
I think you want this:
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2; ;
ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_; ;
The first semi-colon on each line ends the %IF, and the second semi-colon ends the ARRAY statement.
@PaigeMiller wrote:The other problem in your code
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2 %str(;) ; ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ %str(;) ;
When &i=2, then the %IF statements don't execute, and I don't think your ARRAY statements end with a semi-colon.
I think you want this:
ARRAY teer[&nbvareer.] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2; ; ARRAY tev&i.[&&nbvarev&i.] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_; ;
The first semi-colon on each line ends the %IF, and the second semi-colon ends the ARRAY statement.
Okey, I thought we had to put %str( ; ) for the %IF. Thank you for this precision. I'm trying now to resolve my second problem.
On my first loop, I have 17 variables +2 but on the second one I only have 17 variables so I have to modify the
ARRAY teer[&nbvareer.]
ARRAY tev&i.[&&nbvarev&i.]
and more precisely my both macro variables !
I don't see where you are getting different results for &nbvareer or &&nbvarev&i depending on whether or not &I is 1 or 2.
You could use
ARRAY teer &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2; ;
ARRAY tev&i. &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_; ;
in which case SAS figures out how many elements are in each array
@PaigeMiller wrote:I don't see where you are getting different results for &nbvareer or &&nbvarev&i depending on whether or not &I is 1 or 2.
You could use
ARRAY teer &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2; ; ARRAY tev&i. &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_; ;
in which case SAS figures out how many elements are in each array
Aaaaaaaah ok... lol good to know... much easier indeed, I did not know that sas found the number of elements by himself..
Thank you very much ^^
I succeeded :
ARRAY teer[%IF &i = 1 %THEN %eval(&nbvar_eer. +2); %Else &nbvar_eer.;] &list_eer_finale. %IF &i = 1 %THEN ceidfeer_1 ceidfeer_2 ;;
ARRAY tev&i.[%IF &i = 1 %THEN %eval(&&nbvar_ev&i.+2); %Else &&nbvar_ev&i.;] &&list_ev&i._finale. %IF &i = 1 %THEN ceidfev1_1_i1_ ceidfev1_1_i2_ ;;
A little far-fetched but it works haha (i'm still learning about macro language and what you explained to me last time helps me a lot)
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.