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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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