BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

May I know what happened to code 1? I can import perfectly with code 2.  May I know how to make code 1 works? Thanks.

 

 

%LET YYYYMM=201710;

 

/**Code 1**/

%macro checkds(dsn);
%if %sysfunc(exist(&dsn)) %then %do;
PROC IMPORT OUT=sales
DATAFILE=&dsn
DBMS=EXCEL REPLACE;
getnames=yes;
run;
%end;
%else %do;
DATA sales;
ID='';
AMOUNT=0;
RUN;
%end;
%mend checkds;
%checkds("d:\test\book1_&YYYYMM..xlsx")

 

/**code 2**/

PROC IMPORT OUT=SALES
DATAFILE="d:\test\book1_&yyyymm..xlsx"
DBMS=EXCEL REPLACE;
getnames=yes;
run;

2 REPLIES 2
Astounding
PROC Star

As you can see from the macro call, Code #1 expects to receive a quoted path to a file.  The macro checks to see whether that file actually exists.  If it does, it imports the file into a SAS data set named SALES.  If the file doesn't exist, the macro creates a SAS data set named SALES, holding just one observation where ID is blank and AMOUNT is 0.

Kurt_Bremser
Super User

exist() is a function that checks for the existence of a SAS dataset, so it needs an argument that describes such (LIBNAME.DATASET). Since you want to check for an external (non-SAS) file, you need to use the fexist() function.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 843 views
  • 0 likes
  • 3 in conversation