BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ccasagran737
Fluorite | Level 6

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;
1 ACCEPTED SOLUTION

Accepted Solutions
hhinohar
Quartz | Level 8

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;

View solution in original post

9 REPLIES 9
ballardw
Super User

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.

FreelanceReinh
Jade | Level 19

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:

  1. You could replace put(..., 3.-L) with cat(...), but then the default length of var2 would be 200 rather than 3. I would not recommend this.
  2. There are "brute" coders who don't mind notes about automatic character-to-numeric conversion in the log, nor the ugliness of a character variable in an arithmetic expression (cf. Automatic Character-to-Numeric Conversion). So you could get the result without using the INPUT function (i.e., the clean, recommended method for character-to-numeric conversion), but I refuse to write such ugly code.
mkeintz
PROC Star

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;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hhinohar
Quartz | Level 8

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;
Ccasagran737
Fluorite | Level 6
Thank everyone for helping me!!!
FreelanceReinh
Jade | Level 19

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)?

Ksharp
Super User
data have;
	infile datalines missover;
	input var1 $4.;
	datalines;
0345
0002 
0032
;
run;
data want;
	set have;
	var2 = prxchange('s/^0+//',-1,var1);
run;
Ccasagran737
Fluorite | Level 6

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;

 

hhinohar
Quartz | Level 8

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;

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
  • 9 replies
  • 2765 views
  • 2 likes
  • 6 in conversation