hello,
I would like to get only the distinct values for a new variable.
for example,
ID var
1 AABI
2 TIBBC
3 TTIBTIC
so I would like to have a the new variable as output below:
ID var var_new
1 AABI ABI
2 TIBBC TIBC
3 TTIBTIC TIBC
data have;
input ID var $;
cards;
1 AABI
2 TIBBC
3 TTIBTIC
;
data want ;
if _n_=1 then do;
dcl hash H () ;
h.definekey ("k") ;
h.definedone () ;
end;
set have;
length var_new $30;
do _n_=1 to lengthn(var);
k= char(var,_n_);
if h.check()=0 then continue;
var_new=cats(var_new,k);
h.add();
end;
h.clear();
drop k;
run;
What about ABAI? I has 2 A's, but they are not contiguous.
data have;
input ID var $;
cards;
1 AABI
2 TIBBC
3 TTIBTIC
;
data want ;
if _n_=1 then do;
dcl hash H () ;
h.definekey ("k") ;
h.definedone () ;
end;
set have;
length var_new $30;
do _n_=1 to lengthn(var);
k= char(var,_n_);
if h.check()=0 then continue;
var_new=cats(var_new,k);
h.add();
end;
h.clear();
drop k;
run;
thanks, I don't have much knowledge with hash object. that's very nice.
data have;
input ID var $;
cards;
1 AABI
2 TIBBC
3 TTIBTIC
;
data want;
set have;
array x{100} $ 1 _temporary_;
n=0;call missing(of x{*});
do i=1 to length(var);
temp=char(var,i);
if temp not in x then do;n+1;x{n}=temp;end;
end;
want=cats(of x{*});
drop n i temp;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.