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

Hi All,

i have a character date variable in this way

data have;

input sdate $15.;

cards;

NKNKNK

NKMAR2010

04NK2011

NKNK2012

10DEC2009

;

run;

in final dataset i need this into

sdate                        day               mon          year

NK                            NK               NK             NK

NKMAR2010              NK               MAR          2010

04NK2011                 04                 NK             2011

NKNK2012                NK                NK             2012

10DEC2009               10                 DEC           2009

Thanks in Advance

Thanks

Sam

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

data have;

input sdate $15.;

cards;

NKNKNK

NKMAR2010

04NK2011

NKNK2012

10DEC2009

;

data want;

length day $ 2 month $3 year $4;

set have;

day=substr(sdate,1,2);

month=ifc(substr(sdate,3,2)='NK','NK',substr(sdate,3,3));

year=ifc(substr(sdate,(length(sdate)-1),2)='NK','NK',substr(sdate,length(sdate)-3,4));

run;

proc print;run;

Obs    day    month    year    sdate

1     NK      NK      NK      NKNKNK

2     NK      MAR     2010    NKMAR2010

3     04      NK      2011    04NK2011

4     NK      NK      2012    NKNK2012

5     10      DEC     2009    10DEC2009

Linlin

View solution in original post

8 REPLIES 8
Linlin
Lapis Lazuli | Level 10

data have;

input sdate $15.;

cards;

NKNKNK

NKMAR2010

04NK2011

NKNK2012

10DEC2009

;

data want;

length day $ 2 month $3 year $4;

set have;

day=substr(sdate,1,2);

month=ifc(substr(sdate,3,2)='NK','NK',substr(sdate,3,3));

year=ifc(substr(sdate,(length(sdate)-1),2)='NK','NK',substr(sdate,length(sdate)-3,4));

run;

proc print;run;

Obs    day    month    year    sdate

1     NK      NK      NK      NKNKNK

2     NK      MAR     2010    NKMAR2010

3     04      NK      2011    04NK2011

4     NK      NK      2012    NKNK2012

5     10      DEC     2009    10DEC2009

Linlin

Jay_OAG
Calcite | Level 5

Nice..thanks for showing off IFC

PGStats
Opal | Level 21

Or you might try the slightly more robust:

data have;
input sdate $15.;
cards;
NKNKNK
NKMAR2010
04NK2011
NKNK2012
10DEC2009
;
run;

data want;
set have;
length day $ 2 mon $ 3 year $ 4;
sdate=upcase(sdate);
year=substr(sdate, length(trim(sdate))-3, 4);
if notdigit(year)>0 then year="NK";
day=substr(sdate,1,2);
mon=substr(sdate, length(trim(sdate))-6, 3);
if indexw("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", mon) = 0 then mon="NK";
sdate=trim(tranwrd(sdate,"NKNKNK","NK"));
run;

proc print; run;

                           Obs    sdate        day    mon    year

                               1     NK           NK     NK     NK
                               2     NKMAR2010    NK     MAR    2010
                               3     04NK2011     04     NK     2011
                               4     NKNK2012     NK     NK     2012
                               5     10DEC2009    10     DEC    2009

PG
Ksharp
Super User

It is very interesting.

data have;
input sdate $15.;
cards;
NKNKNK
NKMAR2010
04NK2011
NKNK2012
10DEC2009
;
run;


data want(drop=start stop pid position length);
 set have;
pid = prxparse('/nk|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\d+/io');
start = 1;
stop = length(sdate);
call prxnext(pid, start, stop, sdate, position, length);
day = substr(sdate, position, length);
call prxnext(pid, start, stop, sdate, position, length);
month= substr(sdate, position, length);
call prxnext(pid, start, stop, sdate, position, length);
year= substr(sdate, position, length);
run;

Ksharp

PGStats
Opal | Level 21

Very elegant Ksharp. Shows the power of parsing functions, once you master them.

PG
Ksharp
Super User

Thank you. PGStats.

Actually there is another parsing function which can also do it and save some typing words.

data have;
input sdate $15.;
cards;
NKNKNK
NKMAR2010
04NK2011
NKNK2012
10DEC2009
;
run;


data want(drop=pid);
 retain pid;
 if _N_ = 1 then pid = prxparse('/(nk|\d+)(nk|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(nk|\d+)/io');
 set have;
 if prxmatch(pid, sdate) then do;
      date = prxposn(pid, 1, sdate);
      month = prxposn(pid, 2, sdate);
      year = prxposn(pid, 3, sdate);
 end;
run;

Ksharp

sam369
Obsidian | Level 7

Thank you  All,

Ksharp,LinLin,PG Thanks to all of you!!!!!!!

LinLin: IFC, Good thing to know. Previously i dont know this function. Thanks Alot...for letting us to know new functions...

Ksharp: You are real master, simply awesome, prasing functions...... Thanks you so much ....

Thanks

Sam

Linlin
Lapis Lazuli | Level 10


Hi Sam,

You are welcome. That is what I learned from the Forum. It is so great that we can learn from each other.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1518 views
  • 6 likes
  • 5 in conversation