I have tab delimited text file, and tried to import at File>Import data with dab delimited option, and it works. However, the problem is the length of a column is set to be $14, it should be at least 70 though.
So I instead wrote a code to infile as below.
DATA TEST;
INFILE 'C:~~'
DLM='09'X DSD TRUNCOVER firstobs=2;
_infile_ = transtrn(_infile_, "NaN", ".");
length a$100
b$50
c 8
;
input a
b
c
RUN;
It seems work, but shows error at the same time, since missing values of raw data are "NaN" for numeric columns (like c).
My question is either
1. Is there any option for length of column in File>Import data in SAS
or
2. How can I change NaN to numeric missing variable . or "NaN" when infile.
Assuming you're using SAS Enterprise Guide:
"1. Is there any option for length of column in File>Import data in SAS"
You can change column attributes in step 3 (you can change an already existing node via right-click on the node and the select modify).
You can also chose in step 4 to generate more independent import code. When done left click on the generated node and modify the generated code as you wish.
"2. How can I change NaN to numeric missing variable . or "NaN" when infile."
I can think of two options. If you want to use the import wizard then you could first create a custom informat in a separate step and then apply this informat in screen 3 of the import wizard (source informat).
/* option 1 */
proc format;
invalue NaN_to_miss
low-high = [best32.]
'NaN' = .
;
quit;
data sample2;
infile datalines truncover;
input var NaN_to_miss. ;
_infile_ = transtrn(_infile_, "NaN", ".");
datalines;
NaN
99
.
;
run;
Option 2 would be to write your own code. You still could use the informat approach as shown above or you go down the path where you change the value in the input buffer before you map a variable against it. That's basically what you're doing in the code you've posted.
What you've missed in your code and why it's not working for you: You need first to read the data into the input buffer. That's done via an INPUT statement. You also need to use an @ with the input statement to hold the input pointer on the same line of source data so that you can later on use another input statement to map the variables against the buffer (as done in code below).
/* option 2 */
data sample1;
infile datalines truncover;
input @;
_infile_ = transtrn(_infile_, "NaN", ".");
input @1 var best32.;
datalines;
NaN
99
.
;
run;
Assuming you're using SAS Enterprise Guide:
"1. Is there any option for length of column in File>Import data in SAS"
You can change column attributes in step 3 (you can change an already existing node via right-click on the node and the select modify).
You can also chose in step 4 to generate more independent import code. When done left click on the generated node and modify the generated code as you wish.
"2. How can I change NaN to numeric missing variable . or "NaN" when infile."
I can think of two options. If you want to use the import wizard then you could first create a custom informat in a separate step and then apply this informat in screen 3 of the import wizard (source informat).
/* option 1 */
proc format;
invalue NaN_to_miss
low-high = [best32.]
'NaN' = .
;
quit;
data sample2;
infile datalines truncover;
input var NaN_to_miss. ;
_infile_ = transtrn(_infile_, "NaN", ".");
datalines;
NaN
99
.
;
run;
Option 2 would be to write your own code. You still could use the informat approach as shown above or you go down the path where you change the value in the input buffer before you map a variable against it. That's basically what you're doing in the code you've posted.
What you've missed in your code and why it's not working for you: You need first to read the data into the input buffer. That's done via an INPUT statement. You also need to use an @ with the input statement to hold the input pointer on the same line of source data so that you can later on use another input statement to map the variables against the buffer (as done in code below).
/* option 2 */
data sample1;
infile datalines truncover;
input @;
_infile_ = transtrn(_infile_, "NaN", ".");
input @1 var best32.;
datalines;
NaN
99
.
;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.