DATA Step, Macro, Functions and more

Can someone help on the follwing question un-unk-unky....

Reply
Contributor
Posts: 53

Can someone help on the follwing question un-unk-unky....

 

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

 

Trusted Advisor
Posts: 1,586

Re: Can someone help on the follwing question un-unk-unky....

[ Edited ]
Posted in reply to rajeshalwayswel

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;
Trusted Advisor
Posts: 1,586

Re: Can someone help on the follwing question un-unk-unky....

You may need upcase dt.
Contributor
Posts: 53

Re: Can someone help on the follwing question un-unk-unky....

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

Trusted Advisor
Posts: 1,586

Re: Can someone help on the follwing question un-unk-unky....

Posted in reply to rajeshalwayswel

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).

Trusted Advisor
Posts: 1,586

Re: Can someone help on the follwing question un-unk-unky....

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;
  
Contributor
Posts: 53

Re: Can someone help on the follwing question un-unk-unky....

Thanks it work's....
Super User
Posts: 10,046

Re: Can someone help on the follwing question un-unk-unky....

Posted in reply to rajeshalwayswel

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;


Contributor
Posts: 53

Re: Can someone help on the follwing question un-unk-unky....

can you please explain prxchange function I'm little bit confuse in it..
Super User
Posts: 10,046

Re: Can someone help on the follwing question un-unk-unky....

Posted in reply to rajeshalwayswel
Sorry . I have no time to explain PRXCHANGE(), search it at support.sas.com

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

Ask a Question
Discussion stats
  • 9 replies
  • 148 views
  • 4 likes
  • 3 in conversation