DATA Step, Macro, Functions and more

SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

I am using SAS EG 7.15 on Linux using SAS 9.4. For example:

 

data _NULL_;

set in;

file '////out.csv' delimiter=",";

if _n_=1 then put "var1, var2, etc";

put var1, var2, etc;

run;

 

data new;

infile '////out.csv' dlm="," dsd missover firstobs=2 termstr=CLRF;

input var1, var2, etc;

run;

 

The second data step cannot read the records created in the first data step.

 

NOTE: 0 records were read from the infile '////out.csv' .
NOTE: The data set WORK.NEW has 0 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

How do we work around this?


Accepted Solutions
Solution
‎06-19-2018 05:45 PM
Super User
Posts: 23,773

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Posted in reply to bwilsonrelyea

Commas in the PUT statement for one. Picking a location you know you have access to, does this work for you?

 

data _NULL_;

set sashelp.class;

file 'C:\_localdata\temp\out.csv' delimiter=",";

if _n_=1 then put "name, age, sex";

put name age sex;

run;

 

data new;

infile 'C:\_localdata\temp\out.csv' dlm="," dsd missover firstobs=2;

input name $ age sex $;

run;

View solution in original post


All Replies
Super User
Posts: 23,773

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Posted in reply to bwilsonrelyea

Is the code you ran the code you posted?

It's incorrect, the log should have errors? Or post the actual code or a reproducible example.

The premise is correct and this type of logic can work.

 


@bwilsonrelyea wrote:

I am using SAS EG 7.15 on Linux using SAS 9.4. For example:

 

data _NULL_;

set in;

file '////out.csv' delimiter=",";

if _n_=1 then put "var1, var2, etc";

put var1, var2, etc;

run;

 

data new;

infile '////out.csv' dlm="," dsd missover firstobs=2 termstr=CLRF;

input var1, var2, etc;

run;

 

The second data step cannot read the records created in the first data step.

 

NOTE: 0 records were read from the infile '////out.csv' .
NOTE: The data set WORK.NEW has 0 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

How do we work around this?


 

 

New Contributor
Posts: 4

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

There were no error messages. This is just a simplified example. What do I have incorrect other than the etc?

 

I have used many was to create and read a csv file in one program run, including proc import/export and the %ds2cvs macro. No method works. I have to physically go to the file and open the new csv and save it before executing the next statement to read it.

 

Thanks!

Solution
‎06-19-2018 05:45 PM
Super User
Posts: 23,773

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Posted in reply to bwilsonrelyea

Commas in the PUT statement for one. Picking a location you know you have access to, does this work for you?

 

data _NULL_;

set sashelp.class;

file 'C:\_localdata\temp\out.csv' delimiter=",";

if _n_=1 then put "name, age, sex";

put name age sex;

run;

 

data new;

infile 'C:\_localdata\temp\out.csv' dlm="," dsd missover firstobs=2;

input name $ age sex $;

run;
New Contributor
Posts: 4

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Thank you Reeza. This does work. I do not have commas in a put statement in my real code - sorry. I still do not know why my code does not work. It could be because both parts are inside a macro, or that the file name and variable lists are macro variables - but that does not seem likely. I'll investigate some more. It's good to know that it actually works though.

 

NOTE: 19 records were read from the infile '/////out.csv'.

The minimum record length was 9.

The maximum record length was 12.

NOTE: The data set WORK.NEW has 19 observations and 3 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 

Super User
Posts: 23,773

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Posted in reply to bwilsonrelyea

Your example has single quotes, macro variables will only resolve in double quotes but without seeing the code it's anyone's guess.

 


@bwilsonrelyea wrote:

Thank you Reeza. This does work. I do not have commas in a put statement in my real code - sorry. I still do not know why my code does not work. It could be because both parts are inside a macro, or that the file name and variable lists are macro variables - but that does not seem likely. I'll investigate some more. It's good to know that it actually works though.

 

NOTE: 19 records were read from the infile '/////out.csv'.

The minimum record length was 9.

The maximum record length was 12.

NOTE: The data set WORK.NEW has 19 observations and 3 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 


 

New Contributor
Posts: 4

Re: SAS cannot read a csv file that it has just created in the previous datastep. How to fix?

Posted in reply to bwilsonrelyea

I think I have it!

 

It's the

termstr=CRLF.

 

When I delete that it runs.

 

Thanks for all the help.

 

Barbara

 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 90 views
  • 0 likes
  • 2 in conversation