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 Copyright © 2012, SAS Institute Inc. All rights reserved. Today You Will Learn to… Better understand the SAS log Debug your code more easily Work more efficiently 2 Copyright © 2012, SAS Institute Inc. All rights 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… 3 Copyright © 2012, SAS Institute Inc. All rights reserved. In the SAS Log, You Will Encounter: Notes Warnings Errors 4 Copyright © 2012, SAS Institute Inc. All rights 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; 5 Copyright © 2012, SAS Institute Inc. All rights 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; 6 Copyright © 2012, SAS Institute Inc. All rights 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. 7 Copyright © 2012, SAS Institute Inc. All rights 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; 8 Copyright © 2012, SAS Institute Inc. All rights 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. 9 Copyright © 2012, SAS Institute Inc. All rights 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 10 Copyright © 2012, SAS Institute Inc. All rights 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; 11 Copyright © 2012, SAS Institute Inc. All rights 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; 12 Copyright © 2012, SAS Institute Inc. All rights 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; 13 Copyright © 2012, SAS Institute Inc. All rights 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). 14 Copyright © 2012, SAS Institute Inc. All rights 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; The SAS System Obs 1 ssn first_ three 111223333 proc print data=test; run; 15 Copyright © 2012, SAS Institute Inc. All rights 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; The SAS System Obs 1 ssn 111223333 first_ three 111 proc print data=test; run; 16 Copyright © 2012, SAS Institute Inc. All rights reserved. Uninitialized Variable NOTE: Variable FEMELE is uninitialized. data two; set one; if sex=1 then gender=Femele; run; 17 Copyright © 2012, SAS Institute Inc. All rights reserved. Uninitialized Variable (continued) SAS Log: 3652 3653 3654 3655 data two; set one; if sex=1 then gender=Femele; run; NOTE: Variable Femele is uninitialized. 18 Copyright © 2012, SAS Institute Inc. All rights reserved. Uninitialized Variable (continued) Correct syntax: data two; set one; if sex=1 then gender=‘Female’; run; 19 Copyright © 2012, SAS Institute Inc. All rights 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. 20 Copyright © 2012, SAS Institute Inc. All rights reserved. Using a Function Name As an Array Name (continued) SAS log: 43 44 data test; 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. 21 Copyright © 2012, SAS Institute Inc. All rights 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; 22 Copyright © 2012, SAS Institute Inc. All rights 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. 23 Copyright © 2012, SAS Institute Inc. All rights reserved. When Using the Macro Facility, Use These System Options: SYMBOLGEN MPRINT MLOGIC 24 Copyright © 2012, SAS Institute Inc. All rights 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; 25 Copyright © 2012, SAS Institute Inc. All rights 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 &=y %put &=y; 26 Copyright © 2012, SAS Institute Inc. All rights reserved. Masking Special Characters (continued) Corrected code: %let x=a,b,c; %let y=%substr(%bquote(&x),1,1); %put &=y; 27 Copyright © 2012, SAS Institute Inc. All rights reserved. Autocall Macros Used As Macro Functions %CMPRES and %QCMPRES %COMPSTOR %DATATYP %KVERIFY %LEFT and %QLEFT %LOWCASE and %QLOWCASE %TRIM and %QTRIM %VERIFY 28 Copyright © 2012, SAS Institute Inc. All rights 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); 29 Copyright © 2012, SAS Institute Inc. All rights 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. 30 Copyright © 2012, SAS Institute Inc. All rights 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); 31 Copyright © 2012, SAS Institute Inc. All rights 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; 32 Copyright © 2012, SAS Institute Inc. All rights reserved. Using Macro Variables in Filenames (continued) ERROR: Physical file does not exist, c:\class2011csv. datafile=“c:\class2011csv”; 33 Copyright © 2012, SAS Institute Inc. All rights 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; 34 Copyright © 2012, SAS Institute Inc. All rights reserved. Referencing Stored Compiled Macros ERROR: A LOCK IS NOT AVAILABLE FOR LIBREF.SASMACR.CATALOG. 35 Copyright © 2012, SAS Institute Inc. All rights 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 36 Copyright © 2012, SAS Institute Inc. All rights 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. 37 Copyright © 2012, SAS Institute Inc. All rights reserved. Referencing Stored Compiled Macros (continued) 38 Copyright © 2012, SAS Institute Inc. All rights reserved. Invoking a Macro with Parameters Error: More positional parameters found than defined. %macro test(vals); %put &=vals; %mend test; %test(x,y,z) 39 Copyright © 2012, SAS Institute Inc. All rights 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. 40 Copyright © 2012, SAS Institute Inc. All rights reserved. Invoking a Macro with Parameters (continued) Corrected code: %macro test(vals); %put &=vals; %mend test; %test(%str(x,y,z)) 41 Copyright © 2012, SAS Institute Inc. All rights reserved. Questions? Support@SAS.com (919) 677-8008 http://support.sas.com/techsup 42 Copyright © 2012, SAS Institute Inc. All rights reserved.