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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 878 views
  • 0 likes
  • 4 in conversation