DATA Step, Macro, Functions and more

Macro doesn't read the conditional

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Macro doesn't read the conditional

[ Edited ]

Hi! I'm trying to use this macro, but I have a problem with the first "If" conditional, it's like SAS doesn't read the if, and jump all the marked part in the code. Could anyone please tell me what is wrong in the code? (The line 96 has the category Ingreso for the clase column)

 

Thanks in advance!

 

 

%macro recorre;
proc sql noprint;
	SELECT count(*) INTO :total FROM WORK.DATA_DIA;
quit;
%Do I = 96 %to 96;
	proc sql noprint;
		SELECT tipo, clase, horanum INTO :n_tipo, :n_clase, :n_hora FROM WORK.DATA_DIA where n = &I;
	quit;
	%put &n_clase;
*Begin error; %if &n_clase = 'Ingreso' %then %do; %put 'ing'; %if &n_tipo = 'Demanda' %then %do; %put 'dem'; %end; %else %do; %put 'ofe'; %end;
*End error;
%end; %put Fin; %End; %mend recorre; %recorre;
 

 

 


Accepted Solutions
Solution
‎03-05-2018 08:29 AM
PROC Star
Posts: 1,570

Re: Macro doesn't read the conditional

quick look tells me you don't need quotes around values in a macro statement

 

%macro recorre;
proc sql noprint;
	SELECT count(*) INTO :total FROM WORK.DATA_DIA;
quit;
%Do I = 96 %to 96;
	proc sql noprint;
		SELECT tipo, clase, horanum INTO :n_tipo, :n_clase, :n_hora FROM WORK.DATA_DIA where n = &I;
	quit;
	%put &n_clase;
	%if &n_clase = Ingreso %then %do;
		%put 'ing';
		%if &n_tipo = Demanda %then %do;
			%put 'dem';
		%end;
		%else %do;
			%put 'ofe';
		%end;
	%end;
	%put Fin;
%End;
%mend recorre;

%recorre;

View solution in original post


All Replies
Solution
‎03-05-2018 08:29 AM
PROC Star
Posts: 1,570

Re: Macro doesn't read the conditional

quick look tells me you don't need quotes around values in a macro statement

 

%macro recorre;
proc sql noprint;
	SELECT count(*) INTO :total FROM WORK.DATA_DIA;
quit;
%Do I = 96 %to 96;
	proc sql noprint;
		SELECT tipo, clase, horanum INTO :n_tipo, :n_clase, :n_hora FROM WORK.DATA_DIA where n = &I;
	quit;
	%put &n_clase;
	%if &n_clase = Ingreso %then %do;
		%put 'ing';
		%if &n_tipo = Demanda %then %do;
			%put 'dem';
		%end;
		%else %do;
			%put 'ofe';
		%end;
	%end;
	%put Fin;
%End;
%mend recorre;

%recorre;
Contributor
Posts: 66

Re: Macro doesn't read the conditional

Posted in reply to novinosrin
Hi! if you said it for the words 'ing', 'dem' and 'ofe', it's just to probe the code. But I couldn't get those prints.
PROC Star
Posts: 1,570

Re: Macro doesn't read the conditional

I meant for 

 

	%if &n_clase = 'Ingreso' %then %do;
		
		%if &n_tipo = 'Demanda' %then %do;
Contributor
Posts: 66

Re: Macro doesn't read the conditional

Posted in reply to novinosrin
It totally works! Thanks you very much!
Frequent Contributor
Posts: 109

Re: Macro doesn't read the conditional

This is a very common mistake,
We confuse the string comparison in data step if and macro %if.
As mentioned above, the string do not need to be in quote for comparison. Also you do not need to have quotes around strings to %put.
Everything in macro is already character.
Super User
Posts: 23,262

Re: Macro doesn't read the conditional

Posted in reply to novinosrin

Add the debugging options, run the macro again and review the log. If you still can’t get it working post the revised code and log. 

 

Options mprint symbolgen mlogic;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 153 views
  • 4 likes
  • 4 in conversation