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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

5 REPLIES 5
ChrisBrooks
Ammonite | Level 13

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

A very slight modification will make your second code work:

Data test2;
    Infile datalines delimiter=",";
    Input str :$9. num;
    datalines;
abcdefghi,0
zyxwvut,1
;
Autotelic
Obsidian | Level 7
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...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 29519 views
  • 9 likes
  • 4 in conversation