UWA Logo
  Planning Services | Statistics Office | IRU | EIS    
           
University Statistics
Internal
Contacts

SAS Training Code Samples

/* SAS workshop 1.sas, March 2007*/

/* use of colons

The colon has several uses in SAS, all of them worth knowing about.

1                    In a “name prefix list” of variables

In contexts where a list of variables is required, the colon is a wildcard character denoting any

number of characters up the end of the variable name e.g.*/

  keep w: ;

  drop tmp: ;

/*respectively keep all variables whose names begin with “w”, and drop all variables who names begin with “tmp”.

2                    As an operator modifier

The colon behaves in a similar spirit when modifying operators used in comparing character variables,

for example:*/

  if string =: 's' then put 'Begins with "s"';

  if string in: ('r','s','t') then put 'Begins with "r", "s" or "t"' ;

  if string >=: 's' then put 'Begins with "s" or a later letter' ;

/* 3                    As an input format modifier

The colon means “start reading at the next non-blank column” e.g.*/

  data _null_;

    infile cards truncover;

    input name :$20. value :2.;

  cards;

  rob 23

  koon 54

  sonja  9

  ;

  run;

 

/*4  As an output format modifier

The colon means “take as much space as you need and no more”. So for example:

  put name :$20 value 2.;

strips both leading and trailing blanks from NAME, and leaves a single space before printing the numeric VALUE.

It’s a bit like using the CATX function.

5   For creating a macro variable in PROC SQL

For example, this code creates a macro variable WDSCOUNT containing a count of data sets in the WORK library:*/

proc sql;

  select count(*) into :wdscount

  fromdictionary.tables

  where libname='WORK' and memtype='DATA';

quit;

 

data _null_;

  put "Currently &wdscount datasets in WORK library.";

run;

/************/

/* proc sql */

/************/

 

/* comments - inline versus quoted

  you can comment a highlighted block with Ctrl / and uncomment it with Ctrl Shift /

*/

 

proc sql;

  create table temp;

 quit;

 /* to spit out records which have a repeated key, here are two methods - */

 proc sql;

   create table duplicates as

   select blah

   from datasets

   group by key

   having count(key) > 1;

quit;

/* using where on output datasets */

proc summary data=sasuser.houses;

  class style street;

  var _numeric_;

  output sum=

  out=temp(where=(_type_ in (1,3)));

run;

/*You can use SQL functions to summarize data. PROC SQL is more intuitive than

PROC MEANS or PROC SUMMARY, which result in an output table containing more rows

and columns than you need and you have to choose the right _TYPE_ value.

The PROC SQL below summarizes the total number of cases by age group for each year.

*/

proc sql;

  SELECT year, agegrp, SUM(count) AS cases

  FROM stud08.ages

  GROUP BY 1,2;

quit;

 

/* SAS workshop 2.sas

proc sql and proc datasets


Hint - to highlight comments, change the background colour in Tools->Options->Enhanced Editor


Hint 2 - save commonly used code snippets in Tools->Add Abbreviation (eg. program header, proc summary)

*/

/******************************

Program title

Author

**************************/


data en1(drop = course) en2 en3;

set deet07.dx2007;

if 0 <= _N_ <= 1000 then output en1;

else if 1000 < _N_ <= 2000 then output en2;

else if 2000 < _N_ <= 3000 then output en3;

run;

/* 1. list contents of last dataset used, en3 */

proc contents data= _LAST_;

run;

/* 2. list contents of all datasets in the work library */

proc contents data=work._ALL_;

run;

/* 3. list datasets in the work library */

proc contents data=work._ALL_ nods;

run;

/* 4. proc datasets version of above - list all datasets in the specified library */

proc datasets library=work;

quit; run;

/* 5. describe a single dataset */

proc datasets library=work;

contents data=en1;

quit; run;

/* 6. describe all work datasets */

proc datasets;

contents data=_ALL_;

quit; run;

/* 7. copy all datasets in work to stud07, excluding e3*/

proc datasets;

copy in=work out=stud07;

exclude e3;

quit; run;

/* 8. delete everything in work (deletes datasets and formats) */

proc datasets library=work kill;

quit; run;

/* 9. delete just the datasets */

proc datasets library=work memtype=data kill;

quit; run;

/* 10. move all datasets except en3 from stud07 to work */

proc datasets;

copy move in=stud07 out=work;

exclude en3;

quit; run;

/* 11. move just one dataset from stud07 to work */

proc datasets;

copy move in=stud07 out=work;

select en3;

quit; run;

/* 12. delete an individual dataset */

proc datasets library=work;

delete en3;

quit; run;

/* 13. change the name of a dataset */

proc datasets library=work;

change en2=en4;

quit; run;

/* 14. append datasets - note this is quicker than using a set command, as only the appended dataset is read in */

proc datasets library=work;

append out=en4 data=en1;

quit; run;

/* 15. rename a variable */

proc datasets library=work NOlist;

modify en1;

rename student=personid course = course_cd;

run; quit;

/* PROC SQL */

proc sql;

select *

from deet07.en2007 as en

where course like '10%';

quit;

/* an alternative to a proc summary */

proc sql;

create table ensum as

select course, sex, sum(eftsu) as load

from deet07.en2007

group by 1,2;

quit;

/* find units with between 1 and 5 enrolments */

proc sql;

create table small_enrols as

select max(unit) as unit, count(student) as enrols

from deet07.ld2007

group by unit

having count(student) < 6

order by unit;

quit;

/* an sql version of nodupkey */

proc sql;

select distinct(course)

from deet07.en2007;

quit;

/* joins */

/* this is an inner join */

proc sql;

select *

from deet07.en2007 en, deet07.ld2007 ld

where en.student = ld.student

and en.course = ld.course

and en.course like '1012%';

quit;

/* can more explicitly write it as .. */

proc sql;

create table test as

select *

from deet07.en2007 en left join deet07.ld2007 ld

on (en.student = ld.student

and en.course = ld.course)

where en.course like '1012%';

quit;

/* full join is where all records are included, including missing records from left and right */

/* left join includes all records from the left */

Top of Page