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
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
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
Nice..thanks for showing off IFC
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
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
Very elegant Ksharp. Shows the power of parsing functions, once you master them.
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
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
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.