Variables B1STAT JOB_ID are numbers in your example.
So they don't have meaningful leading numbers. So exporting them as 91 and 1 is sensible.
You need to either create a character variable using the Z., or write out the numeric value using the Z. format.
Bad example: the variable b1stat and job_id don't have leading zeros in the dataset. If you want leading zeros you have to attach the appropriate z-format or change the variable to be alphanumeric. Also note that csv-files must not be opened with Excel, as that tool "optimises" the data it reads without further noticing what was changed.
See this:
DATA BCUSTOMERS;
INPUT CUSTNO $ B1STAT JOB_ID;
CARDS;
C19291203 002103 0010
C19291204 000091 0001
C19291205 000000 0101
;
proc print noobs;
run;
Result:
CUSTNO B1STAT JOB_ID C1929120 2103 10 C1929120 91 1 C1929120 0 101
no leading zeroes there.
Now see this:
DATA BCUSTOMERS;
INPUT CUSTNO $ B1STAT :$6. JOB_ID :$4.;
CARDS;
C19291203 002103 0010
C19291204 000091 0001
C19291205 000000 0101
;
proc print noobs;
run;
Result:
CUSTNO B1STAT JOB_ID C1929120 002103 0010 C1929120 000091 0001 C1929120 000000 0101
Codes should always be stored as character.
And yes, never inspect CSV files with Excel. Use a text editor (e.g. Notepad++) for this.
And be very careful with importing strings consisting of numbers solely into Excel.
As it stands now, are the variables character or numeric? If they are numeric, they don't contain leading zeros. In fact, they don't contain any digits at all. SAS stores the value (not a set of characters) for numeric variables. If the variables are character but happen to contain only digits, you should be able to use a somewhat less cumbersome process. At least I think this should work.
Add an observation at the top of the data set. Assign any variable that requires leading zeros a value of 'ABC' in that extra observation. Then when you export, SAS should figure out that these are character variables and preserve leading zeros. After the export, remove the extra row.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.