1
votes

Objective: convert a character variable to numeric with proc sql in sas

Conditions:

  • The input variable has x lenght
  • must keep all 0's in each position
  • THERE ARE SOME FIELDS OF ONLY 0'S

Ex:

The table has one variable with the following:

'00000000'
'00000000'
'00000001'
'20170617'
'20151201'

The expected output is one variable with:

00000000
00000000
00000000
00000001
20170617
20151201

I tried:

PROC SQL;
CREATE TABLE AAA AS
SELECT input(field,z8.) as field_new
FROM input_table
QUIT;

But I get this undesired result:

0
0
0
00000001
20170617
20151201
1
Is the goal removing the quotes? If so, try the TRANSLATE() function.Reeza
I don't understand the question. There is no difference between the number 0 and the number 0000. If you need to keep them different then leave them as character strings.Tom
Z8. is a FORMAT, not in INFORMAT.Tom

1 Answers

0
votes

There is no difference between the number 0 and the number 000. If you want your numbers to print with leading zeros then attach the Z format to the variable.

Let's make an example dataset with a character variable.

data have ;
  length str $8 ;
  input str @@;
cards;
0 00000000 1 00000001 20170617 20151201
;

So to convert the string into a number use the INPUT() function. Use the FORMAT statement to attach a format to control how it prints.

data want ;
 set have;
 num = input(str,F8.);
 format num z8.;
run;

Or in SQL syntax.

proc sql ;
create table want as
  select
    str
  , input(str,F8.) as num format=z8.
  from have
;
quit;

Results:

enter image description here