Unlike a similar question you guys just helped me with - about how to de-dup a list of comma delimited values stored in a variable. My challenge is time is to find a method to prevent the CATX function from ever putting a duplicate date value into the list to begin with. If you have any thoughts on how to alter the code below to accomplish this I'd really appreciate the insights.
The following piece of SAS code takes data rows that looks like this:
BENE FROM_DT (stored in a SAS dataset as SAS date values)
12345 23OCT2010
12345 11NOV2015
12345 11NOV2015
12345 15DEC2011
12345 11NOV2015
And changes the multiple rows into a single row by Bene with the dates all transformed into a single variable named From_Date_List using the CATX function:
BENE FROM_DT_LIST
12345 23OCT2010, 11NOV2015, 11NOV2015, 15DEC2011, 11NOV2015
PROC SORT DATA=FROM_DT_RESHAPE; BY BENE_SK; RUN;
DATA FROM_DT_RESHAPED;
LENGTH FROM_DT_LIST $1000;
DO UNTIL (LAST.BENE);
SET FROM_DT_RESHAPED;
BY BENE;
FROM_DT_LIST = CATX(', ',FROM_DT_LIST, PUT(FROM_DT, DATE9.));
END;
KEEP FROM_DT_LIST BENE;
RUN;
ok, that's easy
data have;
input BENE FROM_DT :date9.;
format from_dt date9.;
cards;
12345 23OCT2010
12345 11NOV2015
12345 11NOV2015
12345 15DEC2011
12345 11NOV2015
;
data want;
array t(999) $9 _temporary_;
call missing(of t(*),n);
do until(last.bene);
set have;
by bene;
temp=put(from_dt,date9.);
if temp not in t then do;
n+1;
t(n)=temp;
end;
end;
want_list=catx(', ',of t(*));
keep bene want_list;
run;
ok, that's easy
data have;
input BENE FROM_DT :date9.;
format from_dt date9.;
cards;
12345 23OCT2010
12345 11NOV2015
12345 11NOV2015
12345 15DEC2011
12345 11NOV2015
;
data want;
array t(999) $9 _temporary_;
call missing(of t(*),n);
do until(last.bene);
set have;
by bene;
temp=put(from_dt,date9.);
if temp not in t then do;
n+1;
t(n)=temp;
end;
end;
want_list=catx(', ',of t(*));
keep bene want_list;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.