Desktop productivity for business analysts and programmers

Reading variables with preceeding zeros

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Reading variables with preceeding zeros

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


Accepted Solutions
Solution
‎08-21-2017 07:12 AM
Super User
Posts: 7,843

Re: Reading variables with preceeding zeros


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
PROC Star
Posts: 326

Re: Reading variables with preceeding zeros

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;

Super User
Posts: 7,843

Re: Reading variables with preceeding zeros

@kiranv_ you need to supply something that tells SAS the necessary lengths, otherwise the character variables will default to length $8.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 326

Re: Reading variables with preceeding zeros

[ Edited ]
Posted in reply to KurtBremser

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

Super User
Posts: 7,843

Re: Reading variables with preceeding zeros

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 11

Re: Reading variables with preceeding zeros

Posted in reply to KurtBremser

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

Solution
‎08-21-2017 07:12 AM
Super User
Posts: 7,843

Re: Reading variables with preceeding zeros


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,979

Re: Reading variables with preceeding zeros

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;
Occasional Contributor
Posts: 11

Re: Reading variables with preceeding zeros

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

Super User
Super User
Posts: 7,979

Re: Reading variables with preceeding zeros

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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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