BookmarkSubscribeRSS Feed

Ready to add codelists by group as a new utility?

Started ‎09-19-2023 by
Modified ‎09-19-2023 by
Views 1,002

Sunil 1.png


Instead of always using PROC FREQ to list unique values by a group variable, why not try using the DATA STEP as an alternative?  The DATA STEP is one the most powerful SAS procedures that enables conditional and group processing, along with statements and functions. With the BY statement, FIRST. and LAST. conditions can be applied to create and update variables.  The RETAIN statement preserves the unique values and the CATX() function concatenates all unique values.  When the record is reached, then the record is saved so that the dataset contains unique records by group variable.


Often programmers find one variable containing a list of unique values easier to review and process instead of PROC FREQ's default rows for each unique value.  In addition, as extra benefits, the unique values can be sorted alphabetically and a count variable can also be added.  Savvy SAS programmers can find multiple applications for this new utility.


* create dataset of unique values by sex;
proc sql;
create table class as
select unique sex, name, count(name) as count
from sashelp.class group by sex order by sex, name;

* concatenate names and then output last sex record;
data nameslst;
 retain nameslst;
 length nameslst $200 count 8.;
 set class; 
 by sex;
 if then nameslst= "";
 if name > '' then nameslst=catx(', ', strip(nameslst), strip(name));
 if then do;
 keep sex nameslst count;
title 'Codelist by Sex Group';
proc print data=nameslst;
 var sex count nameslst;


Version history
Last update:
‎09-19-2023 02:07 PM
Updated by:


Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags