Hi all
please help me to define length for this CHAR variable.
I have char column name "reason_code" which has different length on it and i want to make it 10 for all values.
Have
reason_code
4567898723 /*not changing*/
078654367890 /*removing zeros from beginning and from end*/
09567894563212 /*removing initial 4 digit to make it 10 */
want
reason_code
4567898723
7865436789
7894563212
Thank you
Are those all the situations you can have? What 0's in the middle?
What would you like to do, when given a choice of removing leading or trailing 0's? For example:
0000123456780000
Which characters get removed and which stay?
Just a thought, would you not be better/safer padding up rather than truncated down? I.e. choose a rule, if length < 20 then repeat('0',20-length)||value.
I am trying to make unique length that is 10. i have these three unique situation where i need to make changes.
First case: where length is 10 and no need to change anything
4567898723
Second case: Need to remove leading and trailing zeros to make it 10 digit/length
078654367890 to 7865436789
Third case: Need to remove leading 4 digit to make it 10 digit/length
09567894563212 to 7894563212
Thanks all for your response
OK, here's one approach:
data want;
set have;
do while (length(reason_code) > 10 and substr(reason_code, length(reason_code), 1)='0');
substr(reason_code, length(reason_code), 1) = ' ';
end;
if length(reason_code) <= 10 then return;
do until (length(reason_code)=10);
reason_code = substr(reason_code, 2);
end;
run;
The bottom loop doesn't have to check for zeroes. It removes any character it finds until the string becomes short enough.
Good luck.
Maybe:
Data Have;
Input reason_code $20.;
Datalines;
4567898723
078654367890
09567894563212
;
Run;
Data Want;
Set Have;
If Length (reason_code) gt 10 Then Do;
reason_code=PRXChange("s/^^0*//",1,Reverse(PRXChange("s/^^0*//",1,Compress(Reverse(reason_code)))));
If Length (reason_code) gt 10 Then reason_code=Reverse(Substr(Reverse(Compress(reason_code)),1,10));
End;
Run;
data have;
infile cards truncover;
input var $ 100.;
new_var=prxchange('s/(\d*)([1-9]\d{8}[1-9])(0*)$/$2/oi', -1, strip(var));
cards;
4567898723
078654367890
09567894563212
;
data have;
infile cards truncover;
input var $ 100.;
temp=prxchange('s/0+$//o', -1, strip(var));
new_var=substr(temp,length(temp)-9) ;
cards;
4567898723
078654367890
09567894563212
;
run;
Xia Keshan
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.