BookmarkSubscribeRSS Feed
helloSAS
Obsidian | Level 7

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
4 REPLIES 4
data_null__
Jade | Level 19

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

PGStats
Opal | Level 21

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
Mit
Calcite | Level 5 Mit
Calcite | Level 5


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;

Peter_C
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 778 views
  • 0 likes
  • 5 in conversation