DATA Step, Macro, Functions and more

Macro skipping %goto command

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Macro skipping %goto command

Hello all, I am running a macro that relies on various %goto statments. My issues is that some of them are being ignored and I can't figure out why. Here is the statement with issues. 

 

*ASKS SAS IF CV TYPE OR GD TYPE SHOULD BE SELECTED NEXT CHECKING WHICH HAS THE FIRST OCCURING BLANK;
%if %sysevalf(%superq(VAR1)=,boolean) and %sysevalf(%superq(VAR2) ne ,boolean) %then %goto cvstart;
%if %sysevalf(%superq(VAR1) ne ,boolean) and %sysevalf(%superq(VAR2)=,boolean) %then %goto gdstart;

*GOES TO SPECIFIC POSITION IN MACRO DEPENDING ON PREVIOUS CVTYPE;
%cvstart: %put Curve Iteration &i;
%if "&VAR4"="T" %then %goto tstart;
%if "&VAR4"="C" %then %goto cstart;
%if "&VAR4"="CC" %then %goto ccstart;
%if "&VAR4"="RC" %then %goto rcstart;
%put ERROR IN CURVE;
%goto continue;

*GOES TO SPECIFIC POSITION IN MACRO DEPENDING ON PREVIOUS GDTYPE;
%gdstart: %put Grade Iteration &i;
%if "&VAR7"="S" %then %goto sstart;
%if "&VAR7"="Crest1" %then %goto crest1start;
%if "&VAR7"="Crest2" %then %goto crest2start;
%if "&VAR7"="Sag1" %then %goto sag1start;
%if "&VAR7"="Sag2" %then %goto sag2start;
%put ERROR IN GRADE;
%goto continue;

The goto statement that isn't working is sstart. What should be happening is the macro goes to sstart runs those procedures and go to the next loop. Instead it is skipping the goto statement  printing the error and going to the end of the loop with the continue statement.

 

Below are the macro variables created by the macro on this particular iteration. I use put statements to know this.

VAR1=T
VAR2=
VAR3=2: Rolling
VAR4=T
VAR5=S
VAR6= 13
VAR7=S
VAR8= 1.5
Grade Iteration 13
ERROR IN GRADE

 

I can't figure out why SAS doesn't think "&VAR7"="S". The macro variables are being created by sequentially scanning a line of a dataset each iteration. I even went to dataset the macro variables are being created from and copied and pasted the "S" value into the code. 

 

Thanks 

 

 

 


Accepted Solutions
Solution
‎09-20-2016 12:43 PM
Super User
Posts: 5,082

Re: Macro skipping %goto command

Went back to the DATA set?  That makes me suspect you are using CALL SYMPUT to create these macro variables.  If that's the case, the likely error is that &VAR7 contains trailing blanks.  You could always verify that by changing the %PUT statement to:

 

%put *&VAR7*;

 

In macro language, "S" is not equal to "S   " so the match is not being found.  The simplest solution would be to switch from CALL SYMPUT to CALL SYMPUTX in the DATA step.  That would remove any leading and trailing blanks when creating macro variables.

View solution in original post


All Replies
Solution
‎09-20-2016 12:43 PM
Super User
Posts: 5,082

Re: Macro skipping %goto command

Went back to the DATA set?  That makes me suspect you are using CALL SYMPUT to create these macro variables.  If that's the case, the likely error is that &VAR7 contains trailing blanks.  You could always verify that by changing the %PUT statement to:

 

%put *&VAR7*;

 

In macro language, "S" is not equal to "S   " so the match is not being found.  The simplest solution would be to switch from CALL SYMPUT to CALL SYMPUTX in the DATA step.  That would remove any leading and trailing blanks when creating macro variables.

Contributor
Posts: 45

Re: Macro skipping %goto command

You are correct this is the code I am using 

DATA _NULL_;
	IF 0 THEN SET &SCANFILE NOBS=X;
	CALL SYMPUT('RECCOUNT',X);
STOP;
RUN;
%DO I=1 %TO &RECCOUNT;
DATA _NULL_;
SET &SCANFILE (FIRSTOBS=&I);
	CALL SYMPUT('VAR1',&FIELD1);
	CALL SYMPUT('VAR2',&FIELD2);
	CALL SYMPUT('VAR3',&FIELD3);
	CALL SYMPUT('VAR4',&FIELD4);
	CALL SYMPUT('VAR5',&FIELD5);
	CALL SYMPUT('VAR6',&FIELD6);
	CALL SYMPUT('VAR7',&FIELD7);
	CALL SYMPUT('VAR8',&FIELD8);
STOP;
RUN;

 

I went back and added the *'s and it looks like you are right the macro is putting in some trailing blanks. I'll switch to symputx and see what happens. 

 

VAR1=*T *
VAR2=*S *
VAR3=*2: Rolling *
VAR4=*T *
VAR5=*S *
VAR6=* 2*
VAR7=*S *
VAR8=* 1.5*
Skipped interation 2

Super User
Posts: 10,500

Re: Macro skipping %goto command

You have left out a lot of needed information.

 

You should show the entire macro as the issue could well be elsewhere in your code such as another comparison such as the VAR4 stuff skipping past the evaluation for Var7. With unconditional %goto continue that what ever happens there is a likely place to investigate.

 

So many GOTO statements is what is sometimes referred to as "spaghetti code" because things get so intertwined it can be very difficult to trace out just what happens.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 320 views
  • 1 like
  • 3 in conversation