• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

SAS result is not correct

Hi,

In the code below the instruction
IF %EVAL(%SUBSTR(&mnd,1,2)) EQ "01" THEN DO ;
doesn't work correct.
The result is :
866  %PUT &mnd ;
0111
867  %PUT &vmnd ;
0011
868  %PUT &mnd1 ;
00
it should be :
866  %PUT &lmnd ;
0111
867  %PUT &vmnd ;
1210
868  %PUT &mnd1 ;
12


DATA _NULL_ ;
	%LET periode = 201101 ;
	%LET ljaar    = %SUBSTR(&periode,1,4) ; 
	%LET mnd      = %SUBSTR(&periode,5,2)%SUBSTR(&periode,3,2) ; 
	IF %EVAL(%SUBSTR(&mnd,1,2)) EQ "01" THEN DO ;
		%LET vmnd = 12%EVAL(%SUBSTR(&periode,3,2) - 1) ;
		%LET mnd1 = 12 ;
		END ;	
	ELSE DO ;
		IF %SUBSTR(&mnd,1,2) GE '10' THEN DO ;
			%LET vmnd = %EVAL(%SUBSTR(&periode,5,2) - 1)%EVAL(%SUBSTR(&periode,3,2)) ;
			%LET mnd1 = %EVAL(%SUBSTR(&periode,5,2) - 1) ; ;
		 	END ;
		ELSE DO ;
			%LET vmnd = 0%EVAL(%SUBSTR(&periode,5,2) - 1)%EVAL(%SUBSTR(&periode,3,2)) ;
			%LET mnd1 = 0%EVAL(%SUBSTR(&periode,5,2) - 1) ;
		 	END ;
		END ;
RUN ;

Open in new window

0
sonmic
Asked:
sonmic
1 Solution
 
theartfuldazzlerCommented:
Hi

1.

You need to put the macro part into double quotes:

eg:
IF "%EVAL(%SUBSTR(&mnd,1,2))" EQ "01" THEN DO ;

Note that single quotes will not work, as the %EVAL function will not resolve.


However, a %LET statement will always run in the example, as they are not dependent on whether the DATA STEP IF/THEN statement is true or false. (all 9 %Let statements will run, every time)

It would be best to change this code into a SAS MACRO:




%MACRO KryMaand(periode);
%Global vmnd;
%Global mnd1;
%Global mnd;


%LET ljaar    = %SUBSTR(&periode,1,4) ; 
%LET mnd      = %SUBSTR(&periode,5,2)%SUBSTR(&periode,3,2) ; 
%IF "%EVAL(%SUBSTR(&mnd,1,2))" EQ "01" %THEN %DO ;
	%LET vmnd = 12%EVAL(%SUBSTR(&periode,3,2) - 1) ;
	%LET mnd1 = 12 ;
%END ;	
%ELSE %DO ;
	%IF (%SUBSTR(&mnd,1,2) GE 10) %THEN %DO ;
		%LET vmnd = %EVAL(%SUBSTR(&periode,5,2) - 1)%EVAL(%SUBSTR(&periode,3,2)) ;
		%LET mnd1 = %EVAL(%SUBSTR(&periode,5,2) - 1) ; ;
	%END ;
	%ELSE %DO ;
		%LET vmnd = 0%EVAL(%SUBSTR(&periode,5,2) - 1)%EVAL(%SUBSTR(&periode,3,2)) ;
		%LET mnd1 = 0%EVAL(%SUBSTR(&periode,5,2) - 1) ;
	 %END ;
%END ;

%MEND;

%KryMaand(201101);
%put vmnd = &vmnd;

%put mnd1 = &mnd1;

%put mnd = &mnd;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now