Desktop productivity for business analysts and programmers

Pass CSV file to Macro

Reply
Occasional Contributor
Posts: 9

Pass CSV file to Macro

[ Edited ]

Hi Everyone,

 

I am working EG and i'm trying to pass one csv file to the macro but it is not working Here is my code below.

 

 


%MACRO Operation(MyFile)
DATA WORK."&MyFile"; *file name should come without extension;
LENGTH
Name $ 100
MOBNO $ 20
PhoneNo $ 20
STATE $ 100
CITY $ 100

FORMAT
Name $CHAR100.
MOBNO $CHAR20.
PhoneNo $CHAR20.
STATE $CHAR100.
CITY $CHAR100.


INFORMAT
Name $CHAR100.
MOBNO $CHAR20.
PhoneNo $CHAR20.
STATE $CHAR100.
CITY $CHAR100.

INFILE '/sas/FLATFILES/LEAD/"&MyFile"' * Path of the file should come with extension;
/* LRECL=499
ENCODING="UTF-8"
TERMSTR=CRLF */
DLM='|'
MISSOVER
DSD
FIRSTOBS=2;
INPUT
Name : $CHAR100.
MOBNO : $CHAR20.
PhoneNo : $CHAR20.
STATE : $CHAR100.
CITY : $CHAR100.

RUN;
%MEND;

%Operation("MyFile.csv");

 

 

please if you have any suggestion

PROC Star
Posts: 1,265

Re: Pass CSV file to Macro

Posted in reply to ramanandyadav

First problem: you need a semicolon at the end of the %macro statement.

 

%MACRO Operation(MyFile);

 

Second problem: Too many double quotes. Changing these lines as shown will work.

 

DATA WORK.&MyFile; *file name should come without extension;

INFILE '/sas/FLATFILES/LEAD/&MyFile' * Path of the file should come with extension;

%Operation(MyFile.csv);

 

Third problem: In line

 

DATA WORK.&MyFile; *file name should come without extension;

 

&Myfile is resolving to testfile.txt, which means that your SAS dataset name is work.testfile.txt, which is illegal. Easiest solution is add another macro parameter for the SAS file name:

 

%MACRO Operation(MyFile, MySASFile);

DATA WORK.&MySASFile; *file name should come without extension;

%Operation(MyFile.csv, MyFile);

 

Fourth problem: You need to use double quotes for macro variables to resolve:

 

INFILE "/sas/FLATFILES/LEAD/&MyFile" * Path of the file should come with extension;

 

Fifth problem: Your comment on the INFILE statement is before the semicolon, so it's being picked up as SAS code. You should remove it, or put it after the semicolon:

 

INFILE "/sas/FLATFILES/LEAD/&MyFile"

 

This should move the yardstick for you.

 

Tom

Ask a Question
Discussion stats
  • 1 reply
  • 79 views
  • 0 likes
  • 2 in conversation