Desktop productivity for business analysts and programmers

Datalines with varying length

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Datalines with varying length

Windows 10, SAS Base 9.4, SAS Enterprise Guide 7.1

 

I'm having trouble creating a dataset using datalines.

The code 

Data test1;
    Infile datalines delimiter=",";
    Input str $ num;
    datalines;
abcdefghi,0
zyxwvut,1
;

yields

test1.PNG

The str variable is truncated at the eigth character (by default). In order to try to fix this I tried adjusting the length:

Data test2;
    Infile datalines delimiter=",";
    Input str $9. num;
    datalines;
abcdefghi,0
zyxwvut,1
;

But this also doesn't do what I want since the numbers after the delimiter are being read into str:

test2.PNG

How can I fix this?


Accepted Solutions
Solution
‎10-02-2017 11:30 AM
Valued Guide
Posts: 596

Re: Datalines with varying length

Posted in reply to Autotelic

You just need a slight amendment - use a length statement first.

 

Data test2;
	length str $9;
    Infile datalines delimiter=",";
    Input str $ num;
    datalines;
abcdefghi,0
zyxwvut,1
;

run;

View solution in original post


All Replies
Solution
‎10-02-2017 11:30 AM
Valued Guide
Posts: 596

Re: Datalines with varying length

Posted in reply to Autotelic

You just need a slight amendment - use a length statement first.

 

Data test2;
	length str $9;
    Infile datalines delimiter=",";
    Input str $ num;
    datalines;
abcdefghi,0
zyxwvut,1
;

run;
Super User
Super User
Posts: 9,855

Re: Datalines with varying length

Posted in reply to Autotelic

Just to add to @ChrisBrooks excellent advice, you should read the manual on the sections about defaults so you understand what is going on.  Because you have not previously set the length of a character variable, the default in SAS is to have a length of 8, therefore when the string is read in, only 8 characters can be present.  The solution given explicitly defines the length of that variable to be 9 therefore can read in 9 characters.  It is very important to always specify lengths (and types) on variables before you start to use them, otherwise you may not end up with the result you want.  

With these data imports it is always useful (if from third party but maybe also on other scenarios too) to have a data specification document which details what each fields type, length, format, and possibly code list is, so that anyone picking up the data can easily see the data structure and can interpret it without ambiguity.

Super User
Posts: 10,592

Re: Datalines with varying length

Posted in reply to Autotelic

A very slight modification will make your second code work:

Data test2;
    Infile datalines delimiter=",";
    Input str :$9. num;
    datalines;
abcdefghi,0
zyxwvut,1
;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 40

Re: Datalines with varying length

Posted in reply to KurtBremser
Can you tell me where to read about your solution ":". I can't find it at http://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n0oaql83drile0n141pdacojq97s.htm&docs...
Super User
Posts: 10,592

Re: Datalines with varying length

Posted in reply to Autotelic

Autotelic wrote:
Can you tell me where to read about your solution ":". I can't find it at http://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n0oaql83drile0n141pdacojq97s.htm&docs...

It's part of list input: http://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n0lrz3gb7m9e4rn137op544ddg0v.htm&docs...

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 1005 views
  • 5 likes
  • 4 in conversation