top of page

Learning SAS From Scratch: 7 Things That Tripped Me Up in SAS Studio

4 hours ago
4 min read

I spent this fall writing SAS code for the first time. I came in knowing SQL, Excel, Power BI and Tableau, so I expected the syntax to be the hard part. It wasn't. The syntax is small. What got me were the quiet behaviors, the things SAS does without raising an error.

I worked in SAS Studio on SAS Viya, in the Develop Code and Flows workspace, using SAS's sample tables on cars, national parks, storms and weather stations. I stayed on the code side and have not built a flow yet. Here are the seven things I wish someone had told me on day one.


1. A program is only two kinds of steps

Almost everything is either a DATA step or a PROC step. A DATA step builds or changes a table. A PROC step takes a table and does something with it, like printing or summarizing.

data work.cars_mpg;
    set sashelp.cars;
    AvgMPG = mean(MPG_City, MPG_Highway);
run;

proc means data=work.cars_mpg mean min max maxdec=1;
    var AvgMPG;
    class Type;
run;

The part that took a minute to click is that the DATA step loops for you. That AvgMPG line runs once for every row in the table, and I never wrote a loop.

One DATA step can feed as many PROC steps as you need.


2. A library is a nickname, and SAS forgets it

A library reference points SAS at a folder of tables. It only lasts for the session. I came back one evening and every step failed with "ERROR: Libref PG1 is not assigned".

The real clue was one line higher in the log: "NOTE: Library PG1 does not exist". My LIBNAME statement pointed at a folder name that was one letter off. SAS reported that as a note, not an error, and kept going. Now I read the notes as closely as the errors.

libname pg1 base "~/PG1V2/data/";

3. WHERE runs before OBS=

I assumed (obs=10) meant "take the first 10 rows, then filter them." It works the other way around. SAS applies the WHERE statement first and then stops after 10 matching rows.

proc print data=pg1.np_traffic (obs=10);
    where Count = 0;
run;

You can also filter on a column you never display. Filtering and choosing columns are separate jobs.

Same data, same options. Only one of these is what SAS actually does.


4. Zero rows is an answer

I filtered a weather table for days with more than 20 inches of snowfall and got an empty Results tab. No red text anywhere. The log said "NOTE: No observations were selected".

The code was fine. No single day in that table had that much snow. An empty result and a broken step look exactly the same on screen, so I now run PROC MEANS on a column before I filter it. Knowing the minimum and maximum first saves a lot of guessing at thresholds.


5. A missing number is smaller than every number

This one worries me the most. SAS sorts a missing numeric value below any real number. So a filter for storms with wind of 40 mph or less also returns storms with no wind speed recorded at all.

where MaxWindMPH <= 40 and MaxWindMPH is not missing;

A blank and a low value mean very different things, and the output will not tell you which one you are looking at.

Missing values sit to the left of everything, so any "less than" filter keeps them.


6. An Excel workbook can be a library too

Results that live in the Results tab are no use to someone who does not have SAS. The XLSX engine fixes that by treating a workbook like a library. Every table you write to it becomes a worksheet.

%let outpath=~/PG1V2/output;
libname xlout xlsx "&outpath/snowyweather.xlsx";

proc means data=pg1.np_westweather n mean max nway;
    where Year = 2015;
    var TempMin;
    class Name;
    output out=work.snow_stats mean=Average_TempMin;
run;

data xlout.SNOW_Stats;
    set work.snow_stats;
run;

libname xlout clear;

Two details matter. Macro variables like &outpath only resolve inside double quotes. And the last line is not optional, because SAS holds the file open until the library is cleared.

The summary that ended up in the workbook: average daily low for four western parks in 2015.


7. PROC SQL is real SQL, and joins still fan out

PROC SQL felt like home. It is standard SQL between "proc sql;" and "quit;". The surprise had nothing to do with SAS. I joined a parks table to a traffic table on region, and region is not unique in either one. Every park got paired with every traffic counter in its region.

proc sql;
    select t.ParkCode, s.DayVisits, t.Location
        from pg1.np_summary as s
             inner join pg1.np_traffic as t
             on s.Reg = t.RegCode
        where t.ParkCode = "BLRI";
quit;

The query runs clean and the log says nothing. Checking whether a join key is unique is on me, in SAS the same as anywhere else.

Two parks and three counters in one region become six rows.


What I am taking away

Most of these have the same shape. SAS did exactly what I typed, and nothing turned red. A report that runs is not the same as a report that is right, and the log is where you find the difference.

Next I want to build the same work as a flow, rebuild that join with a DATA step merge to compare the two, and use macro variables to put the date in an export file name.

Comments


bottom of page