Help using Base SAS procedures

character date

Accepted Solution Solved
Reply
Regular Contributor
Posts: 168
Accepted Solution

character date

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


Accepted Solutions
Solution
‎01-18-2012 05:52 PM
Super Contributor
Posts: 1,636

Re: character date

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


All Replies
Solution
‎01-18-2012 05:52 PM
Super Contributor
Posts: 1,636

Re: character date

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

Occasional Contributor
Posts: 14

character date

Nice..thanks for showing off IFC

Respected Advisor
Posts: 4,925

character date

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
Super User
Posts: 10,028

character date

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

Respected Advisor
Posts: 4,925

character date

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

PG
Super User
Posts: 10,028

character date

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

Regular Contributor
Posts: 168

character date

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

Super Contributor
Posts: 1,636

character date


Hi Sam,

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 250 views
  • 6 likes
  • 5 in conversation