BookmarkSubscribeRSS Feed
deleted_user
Not applicable
i use sql to get a macro variable as the name of a file, then want to read this file, but found problems

two programs are as follows; is there any diffence btw them



first,

PROC IMPORT OUT= WORK.t
DATAFILE= "C:\Users\rong\Documents\high-frequency\2006\20061
1\200611sh_csv\sh\20061101\SH000001.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;


second

proc sql noprint;
select code
into: wkbnm
from jj
where row=1;

%put &wkbnm;


PROC IMPORT OUT= WORK.t
DATAFILE= "C:\Users\rong\Documents\high-frequency\2006\20061
1\200611sh_csv\sh\20061101\&wkbnm..csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;


jj is as follows
code row
SH000001 1
SH000002 2
SH000003 3
2 REPLIES 2
Cynthia_sas
Diamond | Level 26
Hi:
You could try turning on the SYMBOLGEN option to see how &wkbnm is being resolved. Depending on the length of &wkbnm in the work.jj dataset, you could be generating a name that looks like this after macro variable resolution:
[pre]
SH000001 .csv (with trailing spaces)
instead of this:
SH000001.csv (without trailing spaces)
[/pre]

Trailing spaces or blanks are not generally stripped from macro variables when you create them with PROC SQL. SYMBOLGEN can help you figure out what's happening. You can also figure out whether this is what's happening in your case with a simple test. Create this new &FNAME macro variables AFTER your PROC SQL step (note that the PROC SQL step ends with a QUIT and not with a RUN). If you want your macro variables available after the step runs, then you must have a "good" step boundary to the SQL step.
[pre]
proc sql noprint;
select code
into: wkbnm
from jj
where row=1;
quit;

%let fname = &wkbnm..csv;
%put &fname;
[/pre]

If &FNAME displays as
[pre]SH000001 .csv[/pre]

then you know that you need to trim the trailing blanks from the macro variable value. (even one space before the ".csv" would result in an incorrect filename reference for your PROC IMPORT syntax.

Trimming the trailing spaces can be done VERY easily, by issuing a %let after the QUIT; statement for the SQL step:

[pre]
proc sql noprint;
select code
into: wkbnm
from jj
where row=1;
quit;

%let wkbnm = &wkbnm;
%let fname = &wkbnm..csv;
%put &fname;
[/pre]

Note the inclusion of the extra %LET statement -- macro variable values assigned with a %LET have the leading and trailing blanks removed from the variable value.

cynthia

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1890 views
  • 0 likes
  • 2 in conversation