BookmarkSubscribeRSS Feed
ravindra2
Fluorite | Level 6

 

 

hi i am having the data like this

 

 

data date1;
input date1 $ 12.;
cards;
30oct2016
12jun2018
oct2016
2016
unk2016
ununk2017
ukoct2018
uuuu
unun
01unk2020
01jan2017
;

 

i need report as

 

30oct2016

12jun2018

oct2016

2016

2016

2017

oct2018

.

.

2020

01jan2017

 

i want like this can any one help me

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can use the function tranwrd to remove parts from a string:

data want;
  set date1;
  date=tranwrd(date,"unk","");
  date=tranwrd(date,"uuuu","");
  date=tranwrd(date,"uk","");
  date=strip(compress(date));
run;

However I am not sure why you have "." in for missings, this is text we are talking about here, dates would need to have all parts of the date to be convertable to date format.

Shmuel
Garnet | Level 18

Try truncate any leading 'u' or 'n' characters, using a loop until first character not in those letters:

 

len = length(date);
ch = substr(date,1,1);
do while (ch in ('u', 'n') and len > 0);
if substr(date,1,3) = 'nov' then leave; date = substr(date,2); len - 1; ch = substr(date,1,1); end;
s_lassen
Meteorite | Level 14

I think that what you want is something like this:

data want;
  set date1;
  if length(date1)=9 then
    if input(date1,?? date9.)=. then
	  date1=substr(date1,3);
  if length(date1)=7 then
    if input(date1,?? monyy7.)=. then
	  date1=substr(date1,4);
  if length(date1)=4 then
    if input(date1,?? 4.)=. then
	  date1='.';
run;

Note the lack of ELSE statements: for once, it is intended (e.g. "ununk2018" is first shortened to "unk2018" and then to "2018").

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 444 views
  • 0 likes
  • 4 in conversation