BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Hi,

I am trying to extract only distinct date from the below character variable.

 

Example:

 

variable abc

has below values and wanted to extract only 3 below distinct dates.

 

final.case_20190712

final.incident_20190405

final.abcd_20190405

final.trans_tarn_20190406

final.incident

 

 

I

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

If the set of words is alpanumeric, 

 

This deals with delim

 



data have;
input abc $30.;
cards;
final.case_20190712
final.incident_20190405
final.abcd_20190405
final.trans_tarn_20190406
final.incident
;
 
data want;
set have;
k=findc(abc,'_','b');
if k>0 then date=substr(abc,k+1);
drop k;
run;

 

 

View solution in original post

5 REPLIES 5
Reeza
Super User
Seems like those are the only numbers so you can consider using COMPRESS with the 'kd' modifiers to keep only digits and then use INPUT() to convert that a numeric SAS date.
novinosrin
Tourmaline | Level 20

If the set of words is alpanumeric, 

 

This deals with delim

 



data have;
input abc $30.;
cards;
final.case_20190712
final.incident_20190405
final.abcd_20190405
final.trans_tarn_20190406
final.incident
;
 
data want;
set have;
k=findc(abc,'_','b');
if k>0 then date=substr(abc,k+1);
drop k;
run;

 

 

novinosrin
Tourmaline | Level 20

@shuchidxt_gmail_com  Oh you want distinct dates

 

proc sql;
create table want as
select distinct ifc(findc(abc,'_','b')>0,substr(abc,findc(abc,'_','b')+1),' ') as dates
from have
having dates>' ';
quit;
shuchidxt_gmail_com
Obsidian | Level 7

Thanks All. it worked

Ksharp
Super User

Just for fun.

 


data have;
input abc $30.;
temp=scan(abc,-1,'_');
if prxmatch('/^\d{8}$/',strip(temp)) then want=temp;
cards;
final.case_20190712
final.incident_20190405
final.abcd_20190405
final.trans_tarn_20190406
final.incident
;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 586 views
  • 2 likes
  • 4 in conversation