SAS (Base SAS)+
*==============================================================================;
* Create sample ADSL data with duplicate treatment-sex combos;
*==============================================================================;
data adsl;
input usubjid $ trt01p $ age sex $;
datalines;
SUBJ001 Placebo 67 M
SUBJ002 DrugA 59 F
SUBJ003 Placebo 72 F
SUBJ004 DrugA 65 M
SUBJ005 Placebo 60 F
SUBJ006 DrugA 55 M
SUBJ007 Placebo 70 M
SUBJ008 DrugA 63 F
;
run;
*==============================================================================;
* SELECT DISTINCT on a single column;
*==============================================================================;
proc sql;
select distinct trt01p
from adsl;
quit;
*==============================================================================;
* SELECT DISTINCT on two columns;
*==============================================================================;
proc sql;
select distinct trt01p, sex
from adsl;
quit;
*==============================================================================;
* Save distinct values into a new dataset;
*==============================================================================;
proc sql;
create table trt_list as
select distinct trt01p
from adsl;
quit;
proc print data=trt_list; run;- To get unique values in SAS, we use
PROC SQLwith theDISTINCTkeyword afterSELECT. SELECT DISTINCT trt01p FROM adsl;returns one row per unique treatment group β Placebo and DrugA.- To find unique combinations across multiple columns, we list them after
DISTINCT:SELECT DISTINCT trt01p, sex FROM adsl;returns one row for each treatment-sex pair. - To save the result for later use, we wrap it in
CREATE TABLE name AS SELECT DISTINCT .... - The output from
SELECT DISTINCTwithoutCREATE TABLEprints directly to the output window β handy for a quick check. - After running, we confirm
trt_listhas exactly two rows: Placebo and DrugA.