420 likes | 695 Views
Why Does SAS ® Say That? What Common DATA Step and Macro Messages Are Trying to Tell Y ou. Charley Mullin and Kevin Russell SAS Institute Inc., Cary, NC USA. Today You Will Learn to…. Better understand the SAS log Debug your code more easily Work more efficiently .
E N D
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
Today You Will Learn to… • Better understand the SAS log • Debug your code more easily • Work more efficiently
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…
In the SAS Log, You Will Encounter: • Notes • Warnings • Errors
Variables with Multiple Definitions ERROR: Variable VAR1 has been defined as both character and numeric. data two; input var1 var2; datalines; 3 4 ; run; data one; input var1 $ var2; datalines; 1 2 ; run; data three; merge one two; by var1 var2; run;
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;
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.
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;
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.
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
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;
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;
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;
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).
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 first_ Obs ssn three 1 111223333 proc print data=test; run;
Automatic Variable Type Conversions (continued) NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column) The SAS System first_ Obs ssn three 1 111223333 111 data test; ssn=111223333; first_three=substr(put(ssn,z9.),1,3); run; proc print data=test; run;
Uninitialized Variable NOTE: Variable FEMELE is uninitialized. data two; set one; if sex=1 then gender=Femele; run;
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.
Uninitialized Variable (continued) Correct syntax: data two; set one; if sex=1 then gender=‘Female’; run;
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.
Using a Function Name As an Array Name(continued) SAS log: 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.
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;
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.
When Using the Macro Facility, Use These System Options: • SYMBOLGEN • MPRINT • MLOGIC
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;
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 %IFcondition 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
Masking Special Characters (continued) Corrected code: %let x=a,b,c; %let y=%substr(%bquote(&x),1,1); %put &=y;
Autocall Macros Used As Macro Functions • %CMPRES and %QCMPRES • %COMPSTOR • %DATATYP • %KVERIFY • %LEFT and %QLEFT • %LOWCASE and %QLOWCASE • %TRIM and %QTRIM • %VERIFY
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);
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 notresolved.
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);
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;
Using Macro Variables in Filenames (continued) ERROR: Physical file does not exist, c:\class2011csv. datafile=“c:\class2011csv”;
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;
Referencing Stored Compiled Macros ERROR: A LOCK IS NOT AVAILABLE FOR LIBREF.SASMACR.CATALOG.
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
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.
Invoking a Macro with Parameters Error: More positional parameters found than defined. %macro test(vals); %put &=vals; %mend test; %test(x,y,z)
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 thandefined. MLOGIC(TEST): Ending execution.
Invoking a Macro with Parameters (continued) Corrected code: %macro test(vals); %put &=vals; %mend test; %test(%str(x,y,z))
Questions? Support@SAS.com (919) 677-8008 http://support.sas.com/techsup