BookmarkSubscribeRSS Feed
mv44
Calcite | Level 5

Hi,

I want to write a macro to import different type of files. Let’s say we have files that are tab-delimited text files as well as CSV files. (“xyz.csv” and “abc.txt”). how do I achieve this?

 

Thanks.

2 REPLIES 2
Reeza
Super User

First figure out all the variations you need and code a manual version. Determine what needs to change for each variation you need and add the corresponding macro variable. 

 

Given PROC IMPORTs current set up I’m not sure you’re going to gain a whole lot by writing this macro though. Is there some problem you’re trying to solve with this macro?

Kurt_Bremser
Super User

Forget your macro, use proc import. You will never be able to reach the flexibility and stability of a SAS-created procedure.

And when you reach the stage of extracting data step code from proc import logs for repeated execution, stay with the data step code. Involving a macro layer will make your code less maintainable.

 

Now, OTOH, if you have a clearly defined basic layout of external files (like the fixed-block format typical on mainframes, or a csv with a given set of datatypes - eg coming from a DB system), you could start to write a macro that automatically creates the import data step off a list of field definitions. But at that stage the question of what delimiter to use would long be solved.

 

I do have such support macros. One handles the data transfer from the server (could be Linux or a mainframe), the other reads a single field and sets type, format, length, and detects all missing-value states. So the import procedure looks like

%make_filename(fileref,infile_name,params);

data want;
infile fileref;
%read_field(name,type,length);
%read_field(name,type,length);
....
run;

This allows me to put an import job side-by-side with the PL-1 include that describes the external file and check if they fit, or where changes are.

("type" in this context is much more specific than just char or num. Think char, varchar, date, uuid, timestamp, binary, zoned, packed decimal, ....)

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 877 views
  • 1 like
  • 3 in conversation