BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Onizuka
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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(;) ;

 

--
Paige Miller
Onizuka
Pyrite | Level 9

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 :

 

  1. number of variable in list_ev1_finale (17 +2)
  2. number of variable in list_ev2_finale (17 +2)

 

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.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Onizuka
Pyrite | Level 9

@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 !

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Onizuka
Pyrite | Level 9

@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 ^^

Onizuka
Pyrite | Level 9

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)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2060 views
  • 3 likes
  • 3 in conversation