BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

Hi,

 

How do you read in only the positions of characters you want if the dataset has tabs, spaces?

 

Example:

data colINPUT;
  length x $10;
  input x $ 3-11;
datalines;
1  abcdefgh  tab spaces something 12345
 aabc                 sf dss
  thirdrecord
run;

How do I create a data from column 3 to column 11 only? So with the sample above, the output dataset should be:

 abcdefgh

abc

thirdreco

 

The code didn't work.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One option might be to use tab as a delimiter. But I can't tell from your posted data whether that makes sense.

 

But the presence of tabs means "column x" is not really very viable as program settings determine how wide a tab would be displayed. With fixed column input, such as you seem to have attempted, a single tab character is  just that, one character. So if the 4th character encountered is a tab then the input would read that tab character as part of the value. You could remove them using COMPRESS function in that case.

 

When it comes to tabs you may have to attach a text file with the values as dealing with such in this forum and/or editors (I have tabs replaced by spaces for instance) gets a bit complicated.

 

Didn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data, normally I would say in the form of a data step. In this case that is part of the issue and an example text file attachment might be a better approach just to be sure that the forum software isn't moving anything.

View solution in original post

4 REPLIES 4
ballardw
Super User

One option might be to use tab as a delimiter. But I can't tell from your posted data whether that makes sense.

 

But the presence of tabs means "column x" is not really very viable as program settings determine how wide a tab would be displayed. With fixed column input, such as you seem to have attempted, a single tab character is  just that, one character. So if the 4th character encountered is a tab then the input would read that tab character as part of the value. You could remove them using COMPRESS function in that case.

 

When it comes to tabs you may have to attach a text file with the values as dealing with such in this forum and/or editors (I have tabs replaced by spaces for instance) gets a bit complicated.

 

Didn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data, normally I would say in the form of a data step. In this case that is part of the issue and an example text file attachment might be a better approach just to be sure that the forum software isn't moving anything.

cosmid
Lapis Lazuli | Level 10
Hi ballardw, looking back at my question, it didn't make sense to me. Thanks for pointing that out.
Tom
Super User Tom
Super User

If someone has accidentally replaced spaces with tabs in your data file (some editors used to do that to save disk space) then perhaps all you need to do is use the EXPANDTABS option on the INFILE statement.  That will replace any tab characters in the data with the right number of spaces to move to the next tab stop.  In that case the tab stops are the "normal" every 8 spaces.

 

Example:

925  filename test temp;
926  data _null_;
927    file test;
928    put '123' '09'x '9.1' '09'x '789';
929  run;

NOTE: The file TEST is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE: 1 record was written to the file (system-specific pathname).
      The minimum record length was 11.
      The maximum record length was 11.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


930
931  data _null_;
932    infile test;
933    input;
934    list;
935  run;

NOTE: The infile TEST is:
      (system-specific pathname),
      (system-specific file attributes)

RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

1   CHAR  123.9.1.789 11
    ZONE  33303230333
    NUMR  12399E19789
NOTE: 1 record was read from the infile (system-specific pathname).
      The minimum record length was 11.
      The maximum record length was 11.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


936
937  data _null_;
938    infile test expandtabs;
939    input;
940    list;
941  run;

NOTE: The infile TEST is:
      (system-specific pathname),
      (system-specific file attributes)

RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
1         123     9.1     789 19
NOTE: 1 record was read from the infile (system-specific pathname).
      The minimum record length was 11.
      The maximum record length was 11.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
cosmid
Lapis Lazuli | Level 10
This expandtabs feature is very good to know. Thank you!

Also, I realized what my issue was and I had asked the wrong question. I had a word document with few columns and I am only interested in one of the column values. When I copy and paste from word document to UNIX SAS, the output included multiple columns, but when I paste them into SAS Windows, the output had the correct result. That's when I realized SAS UNIX and SAS Windows treats tabs differently. But this expandtabs is going to help in the future I am sure of it. I am going to spend a bit time to study it more. Thanks again for the info!

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1149 views
  • 2 likes
  • 3 in conversation