turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Create a dataset with long strings

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-25-2016 05:58 PM

Dear Sas experts,

I am trying to create a simple dataset with 2 char variables, one of each is actually a long string with unpredicted length.

data myLine;

infile datalines delimiter='|';

input myLine $75. lineType $20.;

datalines;

L1. This is my very long line of unpredicted length. | line_one

L2. This is my second very long line of unpredicted length.| line_simple

L3. This is my third, third, third very long line of unpredicted length.| line_another

;

run;

However, the dataset looks weird, e.g.

myLine lineType

This is my very long line of unpredicted length. | line one

The results really depends on the length of the first column (myLine). The numbers .75 and .20 are arbitrary. When I put no numbers (just $) the lines are cut after 8 symbols, I believe.

What is the right way to allow unlimited length (or at least limited by 200 symbols) for the line and to create reliable column values.

Thank you

Accepted Solutions

Solution

01-31-2016
10:18 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to braverju

01-26-2016 01:20 AM

`input myLine $75. lineType $20.;`

This is called formatted input. SAS will read exactly the number of characters into a column that are specified, regardless of delimiters.

If you want to specify maximum lengths for strings, but have variable lengths in the data (and want to honor the delimiters), do this:

```
informat myLine $75. lineType $20.;
input myLine lineType;
```

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to braverju

01-25-2016 06:09 PM

Use informats to specify the maximum length possible. Yes, the default length for a SAS character variable is 8.

```
data myLine;
informat myline $200. lineTYpe $20.;
infile datalines delimiter='|';
input myLine lineType ;
datalines;
L1. This is my very long line of unpredicted length. | line_one
L2. This is my second very long line of unpredicted length.| line_simple
L3. This is my third, third, third very long line of unpredicted length.| line_another
;
run;
```

Solution

01-31-2016
10:18 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to braverju

01-26-2016 01:20 AM

`input myLine $75. lineType $20.;`

This is called formatted input. SAS will read exactly the number of characters into a column that are specified, regardless of delimiters.

If you want to specify maximum lengths for strings, but have variable lengths in the data (and want to honor the delimiters), do this:

```
informat myLine $75. lineType $20.;
input myLine lineType;
```

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

01-31-2016 10:18 AM

Thank you! Got it now.