DATA Step, Macro, Functions and more

Exporting datafile with macro adds (unwanted) extra blanks

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Exporting datafile with macro adds (unwanted) extra blanks

This code is the last part of my macro:

proc export data = allperiods

outfile = "g/entitysheets/test&entityname..csv"

dbms = csv replace;

run;

The data is exported correctly, except I'd like the name to be (for example) testEntityX.csv. But what I'm getting is testEntityX            .csv. How can I get rid of those spaces without going through and deleting them manually?

Thanks for the help!


Accepted Solutions
Solution
‎07-07-2011 11:26 PM
PROC Star
Posts: 7,487

Re: Exporting datafile with macro adds (unwanted) extra blanks

If spaces at the end of your macro variable are really the problem, you could progbably use it like:

g:\entitysheets\%sysfunc(strip(&entityname.)).csv

Art

View solution in original post


All Replies
PROC Star
Posts: 7,487

Re: Exporting datafile with macro adds (unwanted) extra blanks

How about if you use the strip or right function to get rid of the extra spaces?  Of course, dependent on your code, you may have to wrap the function in a %sysfunc().

Art

New Contributor
Posts: 3

Re: Exporting datafile with macro adds (unwanted) extra blanks

I was looking into the strip function, but wasn't sure how I would use it on the file name (I'm new to SAS). Would I just say outfile =  strip("g/entitysheets/test&entityname..csv")?

Solution
‎07-07-2011 11:26 PM
PROC Star
Posts: 7,487

Re: Exporting datafile with macro adds (unwanted) extra blanks

If spaces at the end of your macro variable are really the problem, you could progbably use it like:

g:\entitysheets\%sysfunc(strip(&entityname.)).csv

Art

Frequent Contributor
Frequent Contributor
Posts: 94

Exporting datafile with macro adds (unwanted) extra blanks

Not too different that Art's suggestion, but there's also the %trim() macro function:

data _null_;

format var $20.;

var = 'file_20110708';

call symput('var',var);

run;

%put &var.;

%put filename is "c:\&var..csv";

%put filename is "c:\%trim(&var.).csv";

Super Contributor
Posts: 345

Exporting datafile with macro adds (unwanted) extra blanks

You can add %let entityname = &entityname; right before proc export and all the blanks are gone.

New Contributor
Posts: 3

Exporting datafile with macro adds (unwanted) extra blanks

Thanks for all the help!

Respected Advisor
Posts: 3,799

Exporting datafile with macro adds (unwanted) extra blanks

Everyone has suggested ways to FIX the problem after the fact.  You may want to reflect on how the value was created and address the issue there.  I suspect you created the value with CALL SYMPUT.  SYMPUT does not trim the value of its arguments.  You could use STRIP or TRIM with SYMPUT or use SYMPUTX a new version of SYMPUT that does STRIP the value of its arguments.  SYMPUTX also quietly converts numeric to character, and has a third parameter for SCOPE.

data _null_;

   call symputX('nx',_n_);

   call symput('n',_n_);

   run;

%put NOTE: **&N** **&nx**;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 193 views
  • 7 likes
  • 5 in conversation