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

x
Solved

# SAS result is not correct

Posted on 2011-05-12
Medium Priority
390 Views
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 ;
``````
0
Question by:sonmic
1 Comment

LVL 11

Accepted Solution

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;
``````
0

## Featured Post

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
Course of the Month19 days, 20 hours left to enroll