BookmarkSubscribeRSS Feed
rajeshalwayswel
Pyrite | Level 9

 

If data contains UN----it should replace with -----01

if data contains UNK------it should replace with  -----jan

if data contains UN-UNK-UNKY------ it should be missing....

 

 

data have;
input dt $15.;
cards;
23-jan-2000
un-feb-2001
un-unk-2002
un-unk-unky
un-may-1999
un-mar-2002
un-unk-2010
run;

 

Required Output:

 

23-jan-2000
01-feb-2001
01-jan-2002

01-may-1999
01-mar-2002
01-jan-2010

 

9 REPLIES 9
Shmuel
Garnet | Level 18

As UNKY contains UNK and the last contains UN,

I shall check from the longest to the shortest string:

 

data want;
 set have;
dt = upcase(dt); /* line added */ i = index(dt,'UNKY'); if i > 0 then dt= ' '; /* line was edited as dt is char type */ else do; i = index(dt,'UNK'); if i>0 then substr(dt,4,3) = 'jan'; else do; i = index(dt,'UN'); if i = 1 then substr(dt,1,2) = '01'; end; end; run;
Shmuel
Garnet | Level 18
You may need upcase dt.
rajeshalwayswel
Pyrite | Level 9

sorry it not working any other way just I ran in sas i'm getting exact what I said... Thanks for reply...

Shmuel
Garnet | Level 18

Have you matched case writting:

If UN-UNK-UNKY in input are all lowcase check for un-unk-unky 

or you can precede a line with dt=upcase(dt);  before checking for  UN-UNK-UNKY (in upper case letters).

Shmuel
Garnet | Level 18

Alternatively you can do:

data want;
 set have;
       if substr(dt,1,2) = 'un' then
          substr(dt,1,2) = '01';

      if substr(dt,4,3) = 'unk' then
         substr(dt,4,3) = 'jan';

      if substr(dt,8.4) = 'unky' then dt = ' ';
run;
  
Ksharp
Super User

data have;
input dt $15.;
dt=prxchange('s/\bun\b/01/',1,dt);
dt=prxchange('s/\bunk\b/jan/',1,dt);
want=input(dt,?? date11.);
format want date11.;
cards;
23-jan-2000
un-feb-2001
un-unk-2002
un-unk-unky
un-may-1999
un-mar-2002
un-unk-2010
;
run;


rajeshalwayswel
Pyrite | Level 9
can you please explain prxchange function I'm little bit confuse in it..
Ksharp
Super User
Sorry . I have no time to explain PRXCHANGE(), search it at support.sas.com

Here '\b' stand for the boundary of a word.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 1349 views
  • 4 likes
  • 3 in conversation