BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
PamG
Quartz | Level 8

This seems like a very simple thing but I just can't seem to get it write.  When using the code below, SAS is truncating the last character wherever variable length is greater than 9.  

 

DATA bins;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
DATA bins;
length binnum 8. bin_desc $10.;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;

Formats control the display, at that point it is already read in. SAS defaults to 8 for characters so you want to specify a longer length. 

I used 10 in my code above. 

 


@PamG wrote:

This seems like a very simple thing but I just can't seem to get it write.  When using the code below, SAS is truncating the last character wherever variable length is greater than 9.  

 

DATA bins;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;

 

View solution in original post

5 REPLIES 5
Reeza
Super User
DATA bins;
length binnum 8. bin_desc $10.;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;

Formats control the display, at that point it is already read in. SAS defaults to 8 for characters so you want to specify a longer length. 

I used 10 in my code above. 

 


@PamG wrote:

This seems like a very simple thing but I just can't seem to get it write.  When using the code below, SAS is truncating the last character wherever variable length is greater than 9.  

 

DATA bins;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;

 

PamG
Quartz | Level 8
@Reeza, how do I tell SAS to read more than 8 chararcters?
Reeza
Super User
Sorry, looks like I pasted the wrong code above. See the edited post with the LENGTH statement. You can also use an INFORMAT statement though it's more of an implicit command than explicit with that method.
PamG
Quartz | Level 8
Thank you so much Reeza! I never thought of using LENGTH.
ballardw
Super User

An example with Informat

DATA bins;
informat binnum f8. bin_desc $10.;
INPUT binnum bin_desc $ @@;
FORMAT bin_desc $10.;
DATALINES;
1 [-20,-15) 2 [-5,0) 3 [0,5) 4 [5,10) 5 [10,15) 6 [15,20) 7 [20,25) 8 [25,30) 8 [30,35) 10 [35,40) 
11 [40,45) 12 [45,50) 13 [55,60) 14 [60,65) 15 [65,70) 16 [70,75) 17 [75,80) 18 [80,85) 19 [85,90) 
20 [90,95) 21 [95,100) 22 [100,HI)
;
RUN;

A warning with Datalines: Sometimes  you may have to specify the separator. That would be something like this if your values are separated by | characters (sometimes called a pipe).

infile datalines delimiter='|';

Another sneakier warning: If copying from some documents, word processor or spreadsheets being common culprits, you may have characters that you do not see that are used by those programs internally and your datalines can throw some odd errors. SAS is pretty good about telling you where something goes wrong but what may not be clear. If you see odd error or invalid data messages you may need to go to the data line with the error and delete what appears to be a space and then retype a space.

 

You can also use ATTRIB statement to describe variables before they are read to set information used for reading.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 5 replies
  • 291 views
  • 6 likes
  • 3 in conversation