DATA Step, Macro, Functions and more

filter values

Reply
Frequent Contributor
Posts: 87

filter values

Hi,

I want to delete the values with dates and just keep the last one. For example, I've a field like var1 below and the result should look like New_var1.

var1New_var1
a1.sas7bdata3.sas7bdat
a2.sas7bdatac_bat.sas7bdat
a3.sas7bdatac_det.sas7bdat
ac_bat.sas7bdatacc_20101023.sas7bdat
ac_det.sas7bdatacc_fm.sas7bdat
acc_20101023.sas7bdatact_re.sas7bdat
acc_fm.sas7bdatadds_comp.sas7bdat
act_re.sas7bdatadds_comp_w.sas7bdat
adds_comp.sas7bdata_excl.sas7bdat
adds_comp_w.sas7bdatx_200911.sas7bdat
a_excl.sas7bdaty_201004.sas7bdat
x_200909.sas7bdat
x_200910.sas7bdat
x_200911.sas7bdat
y_200804.sas7bdat
y_200806.sas7bdat
y_200810.sas7bdat
y_200901.sas7bdat
y_200902.sas7bdat
y_200903.sas7bdat
y_200905.sas7bdat
y_200907.sas7bdat
y_200908.sas7bdat
y_200911.sas7bdat
y_200912.sas7bdat
y_201001.sas7bdat
y_201002.sas7bdat
y_201004.sas7bdat
Respected Advisor
Posts: 3,799

Re: filter values

I think it is LAST DOT "name-part" sorted BY "name-part" "numeric-part".  Maybe.

Respected Advisor
Posts: 4,932

Re: filter values

Or maybe in SQL:

proc sql;

create table want as

select var1 as new_var1 from

     (select

          var1,

          compress(scan(var1,1,"."),,"d") as prefix,

          input(compress(scan(var1,1,"."),,"kd"), ? best.) as number

     from have)

group by prefix

having number=max(number);

quit;

PG

PG
Frequent Contributor
Frequent Contributor
Posts: 83

Re: filter values


Hi There

Please try the following codes. This should work. However if you have something like aa1 aa2 or abc1 abc2 then you have to add some mode lines to the code. a similar pattern should work.


data want;
set have;
var1=var;
run;
data want;
set want;
var1=tranwrd(var1,'.sas7bdat','');
if index(var1,'.sas7bda') then delete;
run;

proc sort data=want out=want1;
by descending var1;
run;
data want1;
set want1;
j=index(var1,"_");
k= substr(var1,j+1);
  l   = index(k,"_");
  m   = substr(k,l+1);
  q1   = substr(var1,1,j-1);
  q2   = substr(k,1,l-1);   
  q3   = substr(k,l+1) ;
run;
data want1;
set want1;
var2=k*1;
if j=0 then do;
k=substr(var1,2);
var2=k*1;
q1=substr(var1,1,1);
end;
run;
data want2;
set want1;
q4=q1;
if var2=. then do;
q4=compbl(q1||'_'||q3);
end;
run;
proc sort data=want2;
by  q4;
run;
data want3;
set want2;
by q4;
if first.q4 ;
run;
proc sort data=want3 out= want_final(keep=var);
by var;
run;

Valued Guide
Posts: 2,177

Re: filter values

Is this a case where linguistic sorting might help?

Recently introduced, proc sort sortseq=linguistic( )

And something about number sorting to ensure xxx10 comes after xxx9

Ask a Question
Discussion stats
  • 4 replies
  • 266 views
  • 0 likes
  • 5 in conversation