I have input data that will be numeric and/or character and will be anywhere from 1 to 8 characters in length. For example;
OPTION NOCENTER;
DATA rawdata;
INPUT data $ 1-8;
DATALINES;
CCFAIL
JCLERROR
166
1
2
U500
2947
SE37
;
PROC PRINT DATA = rawdata;
I need the final output to be 8 characters in width, aligned right and leading zeros on any value that begin with a number extending it to a length of 8 characters. For example
JCCFAIL
JCLERROR
00000166
00000001
00000002
U500
00002947
SE37
Any thoughts on how to accomplish this would be greatly appreciated.
proc sql;
select
case (anyalpha(data)>0)
case (notdigit(trim(data))>0)
when (1) then right(data)
else translate(right(data),'0',' ')
end
as data
from rawdata;
quit;
additional edit: the TRANSLATE function translates characters in the first argument. To me, it is counterintuitive that the 3rd arg is translated to the 2nd, but that's how it is.
You have the character values JCLERROR and CCFAIL left-justified in the desired output, but U500 and SE37 are right-justified. What criterion are you using to make that distinction?
Looks like copy/paste got me, desired out put should all be right justified.
data
CCFAIL
JCLERROR
00000166
00000001
00000002
U500
00002947
SE37
proc sql;
select
case (anyalpha(data)>0)
case (notdigit(trim(data))>0)
when (1) then right(data)
else translate(right(data),'0',' ')
end
as data
from rawdata;
quit;
additional edit: the TRANSLATE function translates characters in the first argument. To me, it is counterintuitive that the 3rd arg is translated to the 2nd, but that's how it is.
Thank you!!!!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.