user2493625
user2493625

Reputation: 31

SAS CSV TO CHARACTER LEADING zeros

In SAS 9.3 I need to import a CSV file with my first column having leading zeros. I've reserached and just can't quite figure out how to format the statement. I have done this and messed with it - I know there is a z format that may work but not sure how to incorporate?

data pharmacy;
infile "\\path\June 2013\test.csv"
dsd missover 
/*lrecl=512 pad*/
;
input 
    Field1 $ 1-10
/*  Field2 $*/
;
RUN;

Upvotes: 3

Views: 7662

Answers (2)

user1509107
user1509107

Reputation:

Assuming your data is in the following format:

Field1, Field2
00001,1.2
00002,4.5
00010,189.2
00280546,0
0145605616,6

You were along the right lines regarding Z. format.

If you want to keep Field1 as numeric then just read it as numeric - SAS will ignore the leading zeros. But you can use z10. as the format for Field1. So, when the dataset is created - it will show with leading zeros. Alternatively, if you want to store Field1 as character variable then that too is easy - just read Field1 as numeric and reformat using put(Field1, z10.).

DATA WORK.dummyImport;
    INFILE '/<path>/dummyImport.csv'  MISSOVER DSD FIRSTOBS=2 TERMSTR=CRLF; ;
    INPUT
        Field1           
        Field2           ;
    FORMAT FIELD1 Z10.;
    Field1_char=put(Field1, z10.);
RUN;

PROC PRINT DATA=WORK.DummyImport; RUN;

returns:

Field1  Field2  Field1_char 
0000000001  1.2 0000000001
0000000002  4.5 0000000002
0000000010  189.2   0000000010
0000280546  0   0000280546
0145605616  6   0145605616

Upvotes: 4

Michael Richardson
Michael Richardson

Reputation: 4282

When you're importing a CSV, you definitely want to use the delimiters to your advantage. I find it unlikely that you would want to use column based input statements, e.g. Field1 $ 1-10. Have you tried something as simple as:

data pharmacy;
   infile "\\path\June 2013\test.csv" dsd;
   input Field1 $ Field2 $;
RUN;

Personally, I almost always take the easy way out and just use proc import.

Upvotes: 0

Related Questions