[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SAS result is not correct

Posted on 2011-05-12
1
Medium Priority
?
390 Views
Last Modified: 2013-11-16
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
Comment
Question by:sonmic
1 Comment
 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 2000 total points
ID: 35746583
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

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question