Basic SAS Programming Example
This page is intended to introduce a new SAS user to basic SAS programming. When I say "new SAS user," I don't mean "fresh out of the box" new; however, I mean that the user should be at least somewhat familiar with SAS. Hopefully, this page will deepen one's understanding of SAS. Perhaps it would be best if an experienced SAS user referred to this page when showing a new user how SAS works.
The following is a SAS program written two different ways. The first way is mostly commented out because it relies so much on SAS defaults that it would not run successfully. The second way is written so that no defaults are used. The "no defaults" example should help a new SAS user see what SAS is really doing when processing a program that was written relying on SAS defaults. Think of it as a "behind the scenes" example.
** first way: **;
/********************************************
data fred;
set in.csf9805b (keep= hhid personid age);
if 0 <= age <= 20 then agegp= 1;
if 21 <= age <= 50 then agegp= 2;
if 51 <= age <= 90 then agegp= 3;
data charlie;
merge in.csf9834 (in= a keep= hhid personid d2dqi_r)
fred(in= b);
by hhid personid;
if a = 1 and b = 1;
proc sort;
by agegp;
proc means;
by agegp;
var d2dqi_r;
run;
should be:
********************************************/
** second way: **;
data work.fred;
set in.csf9805b (keep= hhid personid age );
agegp= . ; ** initializes the new variable "agegp" and sets it equal to missing **;
if ( 0 <= age <= 20) = 1 then agegp= 1; ** a true expression evaluates to "1" *;
if (21 <= age <= 50) = 1 then agegp= 2; ** a false expression evaluates to "0" *;
if (51 <= age <= 90) = 1 then agegp= 3;
output work.fred;
run;
data work.charlie;
merge in.csf9834 (in= a keep= hhid personid d2dqi_r)
work.fred (in= b keep= hhid personid agegp);
by hhid personid;
** keep if person is in both data sets **;
if a = 1 and b = 1 then output work.charlie;
run;
proc sort data= work.charlie out= work.johnny;
by agegp;
run;
proc means data= work.johnny;
by agegp;
var d2dqi_r;
run;
Click here for a "side-by-side" explanation of the two ways.


