Hy!
As I've written in the subjet I need to convert a character variabile with padding zero in an another character variabile without padding zero.
I've found this solution but I was wondering if there is an easier one.
Thans for your help!
data have;
infile datalines missover;
input var1 $4.;
datalines;
0345
0002
0032
;
run;
data want;
set have;
var2 = compress(put(input(var1,4.),3.));
run;
why not do as the title says?
data have;
infile datalines missover;
input var1 $4.;
datalines;
0345
0002
0032
;
run;
data want;
set have;
*do as the title says;
var2 = compress(var1,"0");
run;
Not much improvement though
var2 = put(input(var1,4.),3. -L);
The -L with the Put function means to left justify the result, so compress or other space remover is not needed.
However I would likely leave the Put format a 4. just in case you have some values that don't have the leading zero, or explicitly test for such.
Hi @Ccasagran737,
@Ccasagran737 wrote:
I've found this solution but I was wondering if there is an easier one.
If (by "easier") you mean "shorter code," you could go two steps further:
If var1 is always 4 non-blank characters with at least one leading zero, as in your sample data, then just take a substr of var1, starting at the 2nd character:
data have;
infile datalines missover;
input var1 $4.;
datalines;
0345
0002
0032
run;
data want;
set have;
length var2 $3;
var2 = substr(var1,2);
run;
why not do as the title says?
data have;
infile datalines missover;
input var1 $4.;
datalines;
0345
0002
0032
;
run;
data want;
set have;
*do as the title says;
var2 = compress(var1,"0");
run;
Hi @Ccasagran737,
Just to be sure: Do you really want to map, e.g., '0305' (as well as '0035') to '35' (which is what the COMPRESS function would do)?
data have; infile datalines missover; input var1 $4.; datalines; 0345 0002 0032 ; run; data want; set have; var2 = prxchange('s/^0+//',-1,var1); run;
Hi,
today I was testing this solution but I've found a problem.
If the number is 0070 o 0700 I obtain 7 in both case but the first is supposed to be 70 and the second to be 700. I understand there is a mistake in my title because I just want remove leading zero.
data have;
infile datalines missover;
input var1 $4.;
datalines;
0700
0070
;
run;
data test;
set have;
*do as the title says;
var2 = compress(var1,"0");
run;
I'm sorry.
I only read the title and made that answer.
KSharp sent me an excellent solution of using prxchange function.
Thank you @Ksharp .
Other than prxchange function, there is a verify function that returns the position of the first character in source that is not present.
Combined with substr function, it can remove leading zeroes.
data have;
infile datalines missover;
input var1 $4.;
datalines;
0700
0070
;
run;
data want;
set have;
*remove leading blank by regular expression;
var2=prxchange("s/^0+//i",-1,var1);
*VERIFY function returns the position of the first character in source that is not present;
var3=substr(var1,verify(var1,"0"));
run;
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.
Ready to level-up your skills? Choose your own adventure.