Hi
I have a text file which I need to copy to another folder thorugh SAS EG.
I Have used proc import to do so but getting error for field which is numeric in first few records and read as numeric datatype but should be character.
I could use infile statement but does not have time to do so and need some quick solution.
Please suggest.
[Edited 2019-01-15: Changed title from COPY TEXT FILE IN SAS to IMPORT TEXT FILE IN SAS ]
If you know how the variables should be defined then writing your own data step is probably the fastest way to getting a valid dataset. It is not hard to write a data step. Basically I usually just copy the headers, turn them into a LENGTH statement to define the variables add and INFILE statement and an INPUT statement. If there are any date or time values then you might also need to add format and informat statements for those variables.
data want;
length firstvar $10 var2 8 lastvar $5 ;
infile 'myfile.csv' dsd truncover firstobs=2;
input firstvar -- lastvar ;
run;
If you do use PROC IMPORT it will have to guess at how to define the variables based on what data happens to be in the sample file that you are reading. You can increase the GUESSINGROWS setting for PROC IMPORT to have it read more lines of the data before making its guess. That should mean it will run without errors, but if your sample data just happens to all look like numbers then it will still make the column as numeric instead of character.
You can copy the program that PROC IMPORT generates from the SAS log and modify it if you just need to fix a couple of things.
But note that if you are using EG task to both upload and import the file then you might need to switch to use the COPY file task to upload the file and code node to either run PROC IMPORT or run your own data step. The EG IMPORT task might modify the file in the process of uploading it. The COPY FILE task will just copy the file.
See the blog post How to use SAS DATA step to copy a file from anywhere.
However, it should be pretty simple to do in a data step with the infile statement as you suggest yourself. Something like this
data _null_;
infile 'path\in.txt';
file 'path\out.txt';
input;
put _infile_;
run;
1. This code would fail to copy binary files. You need to add option RECFM=N
2. Function FCOPY now makes all these file-copy code examples all over the web obsolete. See here.
If that's just a one off manual task then you could use the Copy Files task.
sorry forget to mentioned need to copy the file in sas dataset format only.
Thanks
Shuchi
Hello,
Do you have access to run X-commands? What environment are you using?
Windows:
X copy H:\SAS\test\test_printto.log H:\SAS;
Unix:
X cp /user/101/text.txt /user/102
I am importing the file to sas dataset.
If you know how the variables should be defined then writing your own data step is probably the fastest way to getting a valid dataset. It is not hard to write a data step. Basically I usually just copy the headers, turn them into a LENGTH statement to define the variables add and INFILE statement and an INPUT statement. If there are any date or time values then you might also need to add format and informat statements for those variables.
data want;
length firstvar $10 var2 8 lastvar $5 ;
infile 'myfile.csv' dsd truncover firstobs=2;
input firstvar -- lastvar ;
run;
If you do use PROC IMPORT it will have to guess at how to define the variables based on what data happens to be in the sample file that you are reading. You can increase the GUESSINGROWS setting for PROC IMPORT to have it read more lines of the data before making its guess. That should mean it will run without errors, but if your sample data just happens to all look like numbers then it will still make the column as numeric instead of character.
You can copy the program that PROC IMPORT generates from the SAS log and modify it if you just need to fix a couple of things.
But note that if you are using EG task to both upload and import the file then you might need to switch to use the COPY file task to upload the file and code node to either run PROC IMPORT or run your own data step. The EG IMPORT task might modify the file in the process of uploading it. The COPY FILE task will just copy the file.
@shuchidxt_gmail_com wrote:
Hi
I Have used proc import to do so but getting error for field which is numeric in first few records and read as numeric datatype but should be character.
Use a large value for the Guessingrows option or place a large value in the wizard used. "Large" here means either the number of records in the file or something like 10000 if the file is really big so that SAS will examine more records before "guessing" what the variable type should be.
You may also encounter truncated character values if the first few are shorter than later values. The guessingrows would help with that as well.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.