Hi Everyone. This might be a simple one, but still, I need to read some data into sas with preceeding zeros as mentioned below: the variables Id and status are character datatype. remaining variables are numeric.
data games;
input nbr1 nbr2 id constant status;
datalines;
013 1297 00000063031560224013 1655003 01
013 1484 00000050031560254205 1655003 01
013 1297 00000040031560224013 1655003 01
013 1484 00000060031560254205 1655003 00
;
run;
Thanks in advance,
Thanu
@Thanu wrote:
Yet, the Zero for the first variable (nbr1) is getting truncated.
That's perfectly OK, because it is numeric (as you said). If you want it to display with leading series, assign it a proper Zw. format.
You just need to add $ after you mention id and status as shown below. $ implies character variable and absence of it as numeric
data games;
input nbr1 nbr2 id $ constant status $;
datalines;
013 1297 00000063031560224013 1655003 01
013 1484 00000050031560254205 1655003 01
013 1297 00000040031560224013 1655003 01
013 1484 00000060031560254205 1655003 00
;
run;
@kiranv_ you need to supply something that tells SAS the necessary lengths, otherwise the character variables will default to length $8.
@Kurt_Bremser You are so right and thanks for correcting me.
Just add informats:
data games;
input nbr1 nbr2 id :$20. constant status :$2.;
datalines;
013 1297 00000063031560224013 1655003 01
013 1484 00000050031560254205 1655003 01
013 1297 00000040031560224013 1655003 01
013 1484 00000060031560254205 1655003 00
;
run;
The colon prevents SAS from using "true" formatted input that would disregard the delimiters.
Yet, the Zero for the first variable (nbr1) is getting truncated.
@Thanu wrote:
Yet, the Zero for the first variable (nbr1) is getting truncated.
That's perfectly OK, because it is numeric (as you said). If you want it to display with leading series, assign it a proper Zw. format.
Can you clarify what the output should be? You say ID is character, but in your example you read it in as number? If you want to display a number with preceeding zeroes then:
data games; input nbr1 nbr2 id constant status; format id z20. status z2.; datalines; 013 1297 00000063031560224013 1655003 01 013 1484 00000050031560254205 1655003 01 013 1297 00000040031560224013 1655003 01 013 1484 00000060031560254205 1655003 00 ; run;
If you just want ID to be character then read it as character:
data games; length id $200; input nbr1 nbr2 id $ constant status; datalines; 013 1297 00000063031560224013 1655003 01 013 1484 00000050031560254205 1655003 01 013 1297 00000040031560224013 1655003 01 013 1484 00000060031560254205 1655003 00 ; run;
ID variable should be a character. When running the 2nd code you posted, the zeros are getting truncated in the first variable (nbr1) it shows only 13 instead of 013
On the input line, $ indicates that the variable should be read as character no dollar means numeric. Numeric values do not contain preceeding zeroes, only by applying the Zx. format can you do this, or by changing to character:
data games; length id $200; input nbr1 $ nbr2 id $ constant status; datalines; 013 1297 00000063031560224013 1655003 01 013 1484 00000050031560254205 1655003 01 013 1297 00000040031560224013 1655003 01 013 1484 00000060031560254205 1655003 00 ; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.