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.
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.
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.
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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.