Upload
phungthu
View
218
Download
0
Embed Size (px)
Citation preview
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
WHY DOES SAS®
SAY THAT?
WHAT COMMON DATA STEP AND
MACRO MESSAGES ARE TRYING
TO TELL YOU
CHARLEY MULLIN AND KEVIN RUSSELL
SAS INSTITUTE INC., CARY, NC USA
PRESENTED BY JAN SQUILLACE
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
TODAY YOU WILL LEARN TO…
•Better understand the SAS log
•Debug your code more easily
•Work more efficiently
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
FIRST, LET’S DISCUSS THE DATA STEP
The benefits of DATA step:
• Gives you the ability to read external files
into SAS data sets
• Enables you to manipulate data into a more
usable form
• Enables you combine data from various
sources
• And more…
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
IN THE SAS LOG, YOU WILL ENCOUNTER:
•Notes
•Warnings
•Errors
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
APPLYING A NUMERIC FORMAT TO A
CHARACTER VARIABLE
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
APPLYING A NUMERIC FORMAT TO A CHARACTER VARIABLE
Input var1 var2 var3 var4 $ var5 $;
Format var4 mmddyy8.;
2681 format var4 mmddyy8.;
--------
48
ERROR 48-59: The format $MMDDYY was not found
or could not be loaded.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
VARIABLES WITH MULTIPLE DEFINITIONS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
VARIABLES WITH MULTIPLE DEFINITIONS
ERROR: Variable VAR1 has been defined as both character and numeric.
data one;
input var1 $ var2;
datalines;
1 2
;
run;
data two;
input var1 var2;
datalines;
3 4
;
run;
data three;
merge one two;
by var1 var2;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
VARIABLES WITH MULTIPLE DEFINITIONS (CONTINUED)
ERROR: Variable VAR1 has been defined as both character and numeric.
data one;
set one(rename=(var1=temp));
var1=input(temp,??8.);
drop temp;
run;
data two;
set two(rename=(var1=temp));
var1=put(temp,8.);
drop temp;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INPUT TO CONCATENATION FUNCTIONS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INPUT TO CONCATENATION FUNCTIONS
NOTE: Argument 1 to function CATS at line N column X is invalid.
NOTE: Further warning from this call to CATS will be suppressed.
WARNING: In a call to the CATS function, the buffer allocated for the
result was not long enough to contain the concatenation of all
the arguments. The correct result would contain 1040
characters, but the actual result may either be truncated to
200 character(s) or be completely blank, depending on the
calling environment. The following note indicates the left-most
argument that caused truncation.
NOTE: Argument 1 to function CATS at line N column 6 is invalid.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INPUT TO CONCATENATION FUNCTIONS (CONTINUED)
data final;
merge group1(rename=(results=r1))
group2(rename=(results=r2))
group3(rename=(results=r3));
results=cats(r1,r2,r3);
run;
data final;
merge group1(rename=(results=r1))
group2(rename=(results=r2))
group3(rename=(results=r3));
length results $ 1040;
results=cats(r1,r2,r3);
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR
• NOTE: Invalid second argument to function
SUBSTR at line N column X.
• NOTE: Invalid third argument to function
SUBSTR at line N column X.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR (CONTINUED)
In typical usage, the three arguments to the
SUBSTR function are:
• The name of the variable from which to
extract characters
• The position in the variable to start
extracting characters
• The number of characters to extract
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR (CONTINUED)
NOTE: Invalid second argument to function SUBSTR at line N column X.
data lab_work;
infile datalines truncover;
input raw $char20.;
machine=substr(strip(raw),1,4);
test_id=substr(strip(raw),6,4);
results=substr(strip(raw),11,4);
datalines;
1234 5678
1234 5678 9012
;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR (CONTINUED)
NOTE: Invalid second argument to function SUBSTR at line N column
X.
data lab_work;
infile datalines truncover;
input raw $char20.;
machine=substr(left(raw),1,4);
test_id=substr(left(raw),6,4);
results=substr(left(raw),11,4);
datalines;
1234 5678
1234 5678 9012
;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
ARGUMENTS TO THE FUNCTION SUBSTR (CONTINUED)
NOTE: Invalid third argument to function SUBSTR at line N column X.
data test;
date=‘24/04/2012’;
year=substr(date,7,10);
run;
data test;
date=‘24/04/2012’;
year=substr(date,7,4);
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOMATIC VARIABLE TYPE CONVERSIONS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOMATIC VARIABLE TYPE CONVERSIONS
• NOTE: Numeric values have been
converted to character values at the places
given by: (Line):(Column).
• NOTE: Character values have been
converted to numeric values at the places
given by: (Line):(Column).
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOMATIC VARIABLE TYPE CONVERSIONS (CONTINUED)
NOTE: Numeric values have been converted to character values at the
places given by: (Line):(Column)
data test;
ssn=111223333;
first_three=substr(ssn,1,3);
run;
proc print data=test;
run;
The SAS System
first_
Obs ssn three
1 111223333
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOMATIC VARIABLE TYPE CONVERSIONS (CONTINUED)
NOTE: Numeric values have been converted to character values at the
places given by: (Line):(Column)
data test;
ssn=111223333;
first_three=substr(put(ssn,z9.),1,3);
run;
proc print data=test;
run;
The SAS System
first_
Obs ssn three
1 111223333 111
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
UNINITIALIZED VARIABLE
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
UNINITIALIZED VARIABLE
NOTE: Variable FEMELE is uninitialized.
data two;
set one;
if sex=1 then gender=Femele;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
UNINITIALIZED VARIABLE (CONTINUED)
SAS Log:
3652 data two;
3653 set one;
3654 if sex=1 then gender=Femele;
3655 run;
NOTE: Variable Femele is uninitialized.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
UNINITIALIZED VARIABLE (CONTINUED)
Correct syntax:
data two;
set one;
if sex=1 then gender=‘Female’;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING A FUNCTION NAME AS AN ARRAY NAME
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING A FUNCTION NAME AS AN ARRAY NAME
NOTE: The array <name> has the same
name as a SAS-supplied or user-defined
function. Parentheses following this name
are treated as array references and not
function references.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING A FUNCTION NAME AS AN ARRAY NAME (CONTINUED)
43 data test;
44 array max(3) max1-max3 (80 90 70);
NOTE: The array max has the same name as a SAS-supplied or
user-defined function. Parentheses following this name are
treated as array references and not function references.
45 max_value=max(of max(*));
ERROR: Too many array subscripts specified for array max.
46 run;
NOTE: The SAS System stopped processing this step because
of errors.
SAS log:
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING A FUNCTION NAME AS AN ARRAY NAME (CONTINUED)
data test;
array max(3) max1-max3 (80 90 70);
max_value=max(of max(*));
run;
data test;
array maxx(3) max1-max3 (80 90 70);
max_value=max(of maxx(*));
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
THE MACRO FACILITY
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
NOW LET’S DISCUSS THE MACRO FACILITY
The macro facility allows you to extend
and customize your code and reduce
the amount of text you must enter.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
WHEN USING THE MACRO FACILITY, USE THESE SYSTEM OPTIONS:
•SYMBOLGEN
•MPRINT
•MLOGIC
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
MASKING SPECIAL CHARACTERS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
MASKING SPECIAL CHARACTERS
ERROR: Macro function %substr has too many arguments. The
excess arguments will be ignored.
%let x=a,b,c;
%let y=%substr(&x,1,1);
%put &=y;
%let x=a,b,c;
%let y=%substr(a,b,c,1,1);
%put &=y;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
MASKING SPECIAL CHARACTERS (CONTINUED)
Complete error in the SAS log:
189 %let x=a,b,c;
190 %let y=%substr(&x,1,1);
SYMBOLGEN: Macro variable X resolves to a,b,c
ERROR: Macro function %SUBSTR has too many arguments.
The excess arguments will be ignored.
ERROR: A character operand was found in the %EVAL function or
%IF condition where a numeric operand is required.
The condition was: b
ERROR: Argument 2 to macro function %SUBSTR is not a number.
ERROR: A character operand was found in the %EVAL function or
%IF condition where a numeric operand is required.
The condition was: c
ERROR: Argument 3 to macro function %SUBSTR is not a number.
191 %put &=y;
&=y
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
MASKING SPECIAL CHARACTERS (CONTINUED)
Corrected code:
%let x=a,b,c;
%let y=%substr(%bquote(&x),1,1);
%put &=y;
This syntax is new for SAS 9.3
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOCALL MACROS USED AS MACRO FUNCTIONS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOCALL MACROS USED AS MACRO FUNCTIONS
• %CMPRES and %QCMPRES
• %COMPSTOR
• %DATATYP
• %KVERIFY
• %LEFT and %QLEFT
• %LOWCASE and %QLOWCASE
• %TRIM and %QTRIM
• %VERIFY
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOCALL MACROS USED AS MACRO FUNCTIONS (CONTINUED)
Example that illustrates a problem:
options mautosource sasautos=(‘c:\mymacs’);
%let x=%str(abc );
%let y=%trim(&x);
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOCALL MACROS USED AS MACRO FUNCTIONS (CONTINUED)
SAS log:
3 options mautosource sasautos=('c:\mymacs');
4 %let x=%str(abc );
5 %let y=%trim(&x);
WARNING: Apparent invocation of macro TRIM not
resolved.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
AUTOCALL MACROS USED AS MACRO FUNCTIONS (CONTINUED)
Correct setting for the SASAUTOS system
option:
options mautosource sasautos=(sasautos, 'c:\mymacs');
%let x=%str(abc );
%let y=%trim(&x);
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING MACRO VARIABLES IN FILENAMES
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING MACRO VARIABLES IN FILENAMES
ERROR: Physical file does not exist, c:\class2011csv.
%let year=2011;
proc import
datafile="C:\class&year.csv"
dbms=csv
out=myclass
replace;
getnames=yes;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING MACRO VARIABLES IN FILENAMES (CONTINUED)
ERROR: Physical file does not exist, c:\class2011csv.
datafile=“c:\class2011csv”;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
USING MACRO VARIABLES IN FILENAMES (CONTINUED)
ERROR: Physical file does not exist, c:\class2011csv.
Here is the correct syntax:
%let year=2011;
proc import datafile=“C:\class&year..csv”
dbms=csv
out=myclass
replace;
getnames=yes;
run;
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REFERENCING STORED COMPILED MACROS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REFERENCING STORED COMPILED MACROS
ERROR: A LOCK IS NOT AVAILABLE FOR LIBREF.SASMACR.CATALOG.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REFERENCING STORED COMPILED MACROS (CONTINUED)
ERROR: A lock is not available for MYMACS.SASMACR.CATALOG.
libname mymacs ‘C:\macros’;
options mstored sasmstore=mymacs;
%macro test / store;
<code>;
%mend test;
%test
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REFERENCING STORED COMPILED MACROS (CONTINUED)
SAS log:
NOTE: The SAS System was unable to open the
macro library referenced by the
SASMSTORE = libref MYMACS.
ERROR: A lock is not available for
MYMACS.SASMACR.CATALOG.
ERROR: A dummy macro will be compiled.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REFERENCING STORED COMPILED MACROS (CONTINUED)
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INVOKING A MACRO WITH PARAMETERS
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INVOKING A MACRO WITH PARAMETERS
Error: More positional parameters found than
defined.
%macro test(vals);
%put &=vals;
%mend test;
%test(x,y,z)
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INVOKING A MACRO WITH PARAMETERS (CONTINUED)
SAS log:
19 %macro test(vals);
20 %put &=vals;
21 %mend test;
22 %test(x,y,z)
MLOGIC(TEST): Beginning execution.
MLOGIC(TEST): Parameter VALS has value x
ERROR: More positional parameters found than
defined.
MLOGIC(TEST): Ending execution.
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
INVOKING A MACRO WITH PARAMETERS (CONTINUED)
Corrected code:
%macro test(vals);
%put &=vals;
%mend test;
%test(%str(x,y,z))
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
REVIEW
• Variables with multiple definitions
• Concatenation function arguments
• Arguments to SUBSTR function
• Automatic variable type conversion
• Uninitialized variables
• Function name conflict with array name
• Macro debugging options SYMBOLGEN MPRINT MLOGIC
• Autocall macros used as macro functions
• SASAUTOS system option conflicts
• Macro variables used in filenames
• Stored compiled macros
• Macro parameters
Company Confidential - For Internal Use Only
Copyright © 2013, SAS Insti tute Inc. Al l r ights reserved.
Paper 248-2012
Why Does SAS® Say That? What Common DATA Step and Macro
Messages Are Trying to Tell You
Email: [email protected]
(919) 677-8008
http://support.sas.com/techsup
http://support.sas.com/resources