BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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 ]

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

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;
ChrisNZ
Tourmaline | Level 20

@PeterClemmensen 

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.

Patrick
Opal | Level 21

If that's just a one off manual task then you could use the Copy Files task.

Capture.JPG

shuchidxt_gmail_com
Obsidian | Level 7

sorry forget to mentioned need to copy the file in sas dataset format only.

 

Thanks

Shuchi

SuryaKiran
Meteorite | Level 14

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
Thanks,
Suryakiran
Reeza
Super User
Are you copying a file or reading it into a SAS data set. If your pROC IMPORT fails, it also provides the code in the log. The faster way to get it correct is to copy that program and modify it to get what you want.
shuchidxt_gmail_com
Obsidian | Level 7

I am importing the file to sas dataset.

Reeza
Super User
Then use my suggestion of using PROC IMPORT and then modifying the code from the log. It's your fastest solution that will give you accurate data.
Tom
Super User Tom
Super User

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.

ballardw
Super User

@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.

 

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 3910 views
  • 6 likes
  • 8 in conversation