BookmarkSubscribeRSS Feed
Rickyboy
Calcite | Level 5

Good morning

 

i m trying to write a macro that imports some csv files and then appends them

 

%macro import(start,end);

 

proc sql;

%do i=&start. %to &end.;

 

PROC IMPORT

DATAFILE="C\data&i..csv"

OUT=work.data&i. ;

 

run;

%END;

 

quit;

%mend;

 

 

%import(start=1, end=22)

 

It works

except for the fact that sometimes it imports some of the columns as text and some other as values , which means that the successive APPEND procedure fails because the columns are not the same

 

is there a way I can specify the informat for these 2 columns or alternatively import everything as text and then change it into my desired format once I have appended the file.

 

I understand this is due to the algorithm used to determine if a column is text or number used by sas

 

I have looked at the documentation for sas and I m not sure any of the option listed are relevant

 

Kind regards

A frustrated user !

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Drop the proc import - this is a guessing procedure hence why you get different results.

Write a datastep import (you can use the output from the proc import to save time typing it all in).  In this you can specify formats, informats, lengths, labels etc. 

Using wildcard notation you can read all the csv files in one go.  See:

https://communities.sas.com/t5/Base-SAS-Programming/Help-merging-multiple-CSV-files-to-a-dataset/td-...

Reeza
Super User

Try GUESSINGROWS=MAX but I would strongly recommend using the data step approach. 


Run a PROC IMPORT once, copy the code from the log. Make sure all types/formats are correct and use that inside your macro instead.

 


@Rickyboy wrote:

Good morning

 

i m trying to write a macro that imports some csv files and then appends them

 

%macro import(start,end);

 

proc sql;

%do i=&start. %to &end.;

 

PROC IMPORT

DATAFILE="C\data&i..csv"

OUT=work.data&i. ;

 

run;

%END;

 

quit;

%mend;

 

 

%import(start=1, end=22)

 

It works

except for the fact that sometimes it imports some of the columns as text and some other as values , which means that the successive APPEND procedure fails because the columns are not the same

 

is there a way I can specify the informat for these 2 columns or alternatively import everything as text and then change it into my desired format once I have appended the file.

 

I understand this is due to the algorithm used to determine if a column is text or number used by sas

 

I have looked at the documentation for sas and I m not sure any of the option listed are relevant

 

Kind regards

A frustrated user !


 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1456 views
  • 0 likes
  • 3 in conversation