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

I run the below codes but could not import the external file despite meeting the criteria.  Anyone can help? Thanks.

 

data test;
input brand $;
cards;
BMW
;
run;

 

proc sql noprint;
select distinct brand
into :brand separated by ''
from test;
quit;

 

%let dsid=%sysfunc(open(test));
%let num=%sysfunc(attrn(&dsid,nobs));
%let rc=%sysfunc(close(&dsid));

%put &dsid;
%put #
%put &rc;

 

/**if number of row = 1 and excel file exist**/
%macro checkds;
%if &num eq 1 or %sysfunc(fileexist(&brand_.xlsx)) %then %do;
PROC IMPORT OUT=brand
DATAFILE="D:\&brand..xlsx"
DBMS=EXCEL REPLACE;
GETNAMES=YES;
run;
%end;
%else %do;
stop;
%end;
%mend checkds;

2 REPLIES 2
Tom
Super User Tom
Super User

Your call to FILEEXIST() is looking for a different file than you are using in your PROC IMPORT code.

%if &num eq 1 or %sysfunc(fileexist(&brand_.xlsx)) %then %do;
PROC IMPORT OUT=brand DATAFILE="D:\&brand..xlsx"
...

The first one is using a macro variable name BRAND_ and the second is using a macro variable named BRAND.

 

The top part of your program is a little confusing also. 

Are you trying to concatenate all of the values of BRAND into a single macro variable? 

Or do you expect your input dataset to have only one value of BRAND?

Or are you trying to loop over the lists of brands in the dataset and import multiple XLSX files?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So, your using a guessing procedure to guess what the data should like as you don't know.  You are using the worst possible data format file which has no structural rigidity.  And then to compound it you don't even know if the file exists? 

And this process is going to work? (clue, answer is no it will not work).

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