BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

Not able to remove trailing blanks from repnum variable.

Piece of code:-

%LET project= XY_ABCD0021_XXXX;
%GLOBAL project repnum business ;
DATA _NULL_;
repnum=substr("&project",1,11);
business=substr("&project",1,2);
CALL SYMPUT("repnum",repnum);
CALL SYMPUT("business",business);
RUN;


FILENAME outfile "c:\test\&repnum..txt";

PROC PRINTTO FILE=outfile NEW;
RUN;

In this piece code I want to create text file with the name XY_ABCD0021.txt
But when I ran this code my file name is saved with trailing blanks after XY_ABCD0021
.txt

So can you give me any solution so that i can remove these trailing blanks.

Message was edited by: Sunil Gandhi Message was edited by: Sunil Gandhi
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:
This is not really an ODS or reporting procedure question. However, when you use the DATA step and CALL SYMPUT to make your macro variables, you are responsible for trimming the data step variables yourself.

My preferred method is to just use %SUBSTR and the %LET -- which the macro facility usually trims for you. Compare the LOG (and the %PUT output) for both of these examples. For more help with the Macro coding, contact Tech Suport.

cynthia
[pre]
*** 1) only use %LET and %SUBSTR;
*** use %LET statement and NOT datastep;
*** %LET will strip trailing blanks for you;

%LET project= XY_ABCD0021_XXXX;
%GLOBAL project repnum business ;
%put ------->;
%let repnum = %substr(&project,1,11);
%put &repnum;
%let business = %substr(&project,1,2);
%put &business;

%let newname = &repnum..txt;
%put &newname (without any "trim");
%put ------->;

FILENAME outfile "c:\temp\&repnum..txt";

PROC PRINTTO FILE=outfile NEW;
RUN;

proc print data=sashelp.class;
title "will be saved to &repnum..txt";
run;

proc printto; run;

*** 2) Use DATA step and SYMPUT, but use TRIM function, too.;
%LET project= ZZ_ZYXW0042_AAAA;

DATA _NULL_;
repnum2=substr("&project",1,11);
business2=substr("&project",1,2);
CALL SYMPUT("repnum2",trim(repnum2));
CALL SYMPUT("business2",trim(business2));
RUN;

%put -------> using trim function with call symput;

%put &repnum2;

%put &business2;

%let newname2 = &repnum2..txt;
%put &newname2 (with "trim" and call symput);
%put ------->;

FILENAME outfile2 "c:\temp\&repnum2..txt";

PROC PRINTTO FILE=outfile2 NEW;
RUN;

proc print data=sashelp.class;
title "will be saved to &repnum2..txt";
run;

proc printto; run;

[/pre]

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
  • 1 reply
  • 671 views
  • 0 likes
  • 2 in conversation