Manage Data
Functions, arrays, and iteration
Most manipulation happens in the DATA step, one observation at a time. Know the function families: character (SUBSTR, SCAN, INDEX, FIND, COMPRESS, STRIP, TRIM, CATX, UPCASE, LENGTH), numeric (SUM, MEAN, N, NMISS, ROUND, INT), date/time (TODAY, MDY, INTCK, INTNX, YRDIF, DATEPART), and conversion (PUT for value-to-text, INPUT for text-to-value). ARRAY declarations with DO loops process many variables at once. RETAIN carries a value into the next iteration. FIRST.var and LAST.var exist only when the DATA step contains a BY statement on sorted data.
Traps
The SUM function ignores missing values, while the expression x = x + y propagates them; the sum statement x + y retains the variable automatically and ignores missing values. SUBSTR and SCAN positions are 1-based. A character variable first created in the DATA step defaults to length 200, so assign a LENGTH statement when you want something shorter. In PROC SORT, NODUPKEY compares only the BY variables while NODUP compares the entire row, and OUT= suppresses the default replacement of the input table. A format created with PROC FORMAT must be reachable through the FMTSEARCH path to be used in another session. TRANSPOSE and PROC SQL reshape data when a DATA step would be clumsy.
Rule of thumb
Sort before BY. If a BY variable is not sorted, SAS stops with "BY variables are not properly sorted" and produces no output.
proc sort data=work.sales out=work.sorted;
by region descending amount;
run;
data work.top;
set work.sorted;
by region;
if first.region;
run;