From: Sdlentertd on
On Jul 20, 10:12 am, Ed <es...(a)ix.netcom.com> wrote:
> have you tried new_char=substr(put(input(old_num,12.0),z12.),6,7)?
>
>
>
> Sdlentertd wrote:
>
> > I have a numeric field and i need to convert it into character field
> > that is 7 digits.
> > so if i have numeric 1234567 then the character will be 1234567
> > if i have numeric 123456 then the character should be with a leading
> > 0 : 0123456
> > If i have numeric 12345678 then the character field should be 2345678
> > - dropping the first digit.
> > so if it's 7 digits then it's fine, if it's less than 7 digits add
> > leading 0s and if it's more than 7 then only grab 7 digits starting
> > from the right.
>
> > i have this code but it only converts to character and adds leading 0s
> > where needed, but what if it's more than 7 i don't know how to tell
> > SAS to grab first 7 from the right.
> > New_char = put(input(Old_num,12.0),z7.);
>
> > Thank you- Hide quoted text -
>
> - Show quoted text -

Ed, your code seems to work perfectly, Thank you
From: Barry Schwarz on
On Tue, 20 Jul 2010 10:23:09 -0700 (PDT), Sdlentertd
<sdlentertd(a)gmail.com> wrote:

>On Jul 20, 10:12�am, Ed <es...(a)ix.netcom.com> wrote:
>> have you tried new_char=substr(put(input(old_num,12.0),z12.),6,7)?
>>
>>
>>
>> Sdlentertd wrote:
>>
>> > I have a numeric field and i need to convert it into character field
>> > that is 7 digits.
>> > so if i have numeric 1234567 then the character will be 1234567
>> > if i have numeric 123456 then the character should be with a leading
>> > 0 : 0123456
>> > If i have numeric 12345678 then the character field should be 2345678
>> > - dropping the first digit.
>> > so if it's 7 digits then it's fine, if it's less than 7 digits add
>> > leading 0s and if it's more than 7 then only grab 7 digits starting
>> > from the right.
>>
>> > i have this code but it only converts to character and adds leading 0s
>> > where needed, but what if it's more than 7 i don't know how to tell
>> > SAS to grab first 7 from the right.
>> > New_char = put(input(Old_num,12.0),z7.);
>>
>> > Thank you- Hide quoted text -
>>
>> - Show quoted text -
>
>Ed, your code seems to work perfectly, Thank you

You stated that old_num was a numeric variable. The input function
requires a character expression. Why are you (and everyone who
followed you) passing a numeric expression to input? Didn't your log
indicate that "unnecessary" conversions were being done for you?

What happens if you use
new_char = substr(put(old_num,z12.),6,7);

--
Remove del for email
From: PeterC on
On Jul 20, 5:51 pm, Sdlentertd <sdlente...(a)gmail.com> wrote:
> I have a numeric field and i need to convert it into character field
> that is 7 digits.
> so if i have numeric 1234567 then the character will be 1234567
> if i have numeric 123456 then the character should be with a leading
> 0 : 0123456
> If i have numeric 12345678 then the character field should be 2345678
> - dropping the first digit.
> so if it's 7 digits then it's fine, if it's less than 7 digits add
> leading 0s and if it's more than 7 then only grab 7 digits starting
> from the right.
>
> i have this code but it only converts to character and adds leading 0s
> where needed, but what if it's more than 7 i don't know how to tell
> SAS to grab first 7 from the right.
> New_char = put(input(Old_num,12.0),z7.);
>
> Thank you

extracting the last 7 at the right hand end of a whole number
(ignoring decimal fractions) is provided by the int( mod( number,
1e7 ) ) functions. Mod() provides a remainder after dividing the first
parameter by the second. If you are only dealing with integers, then
the int() is not needed. As you want to convert the result to a string
with leading zeros use put() with the Zw. format ....
string = put( mod( number, 1e7 ), z7. ) ;

The 1e7 notation makes it easier to see how many digits are wanted,
compared with 10000000, where my old eyes get bored counting ;-)
From: RolandRB on
On Jul 20, 6:51 pm, Sdlentertd <sdlente...(a)gmail.com> wrote:
> I have a numeric field and i need to convert it into character field
> that is 7 digits.
> so if i have numeric 1234567 then the character will be 1234567
> if i have numeric 123456 then the character should be with a leading
> 0 : 0123456
> If i have numeric 12345678 then the character field should be 2345678
> - dropping the first digit.
> so if it's 7 digits then it's fine, if it's less than 7 digits add
> leading 0s and if it's more than 7 then only grab 7 digits starting
> from the right.
>
> i have this code but it only converts to character and adds leading 0s
> where needed, but what if it's more than 7 i don't know how to tell
> SAS to grab first 7 from the right.
> New_char = put(input(Old_num,12.0),z7.);
>
> Thank you

data _null_;
x=123456789;
str=substr(put(x,z14.),8);
put str=;
run;