BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Thanu
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@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.

View solution in original post

9 REPLIES 9
kiranv_
Rhodochrosite | Level 12

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_
Rhodochrosite | Level 12

@Kurt_Bremser You are so right and thanks for correcting me.

Kurt_Bremser
Super User

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.

Thanu
Calcite | Level 5

Yet, the Zero for the first variable (nbr1) is getting truncated. 

Kurt_Bremser
Super User

@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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Thanu
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2152 views
  • 0 likes
  • 4 in conversation