BookmarkSubscribeRSS Feed
cho16
Obsidian | Level 7

Hi Team

 

I am running the below code and am getting space in the output. The &comp. is the global variable.

Is there any way that we can remove the space at the output.

 

data _null_;
set &outdat.;
file "&drv2./test/Current/&benpk._&comp._00.txt" ls = 4000 mod;

put '"' name +(-1) '",'
'"' fname +(-1) '",'
'"'zip +(-1) '",'

run;

 

E_42 _00.txt

 

NEED:

E_42_00.txt

 

Thank You

5 REPLIES 5
ballardw
Super User

 

HOW do you create &comp? An unwanted space would mean the approach to creating it needs to be addressed. Prevent not fix is a better approach.

 

The %trim macro function might work unless the "space" shown in the name is a different null character.

%let comp=%trim(&comp.);

 

Tom
Super User Tom
Super User

If COMP just has an regular space without macro quoting it is much easier to just use the %LET statement itself to remove the trailing/leading spaces.

%let comp=∁

I don't really like to use %TRIM() as it is an autocall macro and not an actual macro function.  So if someone has accidentally left the SYMBOLGEN and/or MLOGIC options turned on your SAS log can get cluttered with a lot of extra lines depending on the number of trailing spaces it has to remove.

cho16
Obsidian | Level 7
Yes i tried the trim function, still getting a space.

Is there any option to change the output text file name from SAS data step ?
Tom
Super User Tom
Super User

The TRIM() function works on actual variables or strings. Not on macro variables.  And if you assign the result back to a variable then the spaces will return if the result is shorter than the defined length of the variable.  SAS variables are fixed length so short values are padded with spaces.

 

Use the CALL SYMPUTX() when generating macro variables from a data step.

Use the TRIMMED keyword when generating macro variable from PROC SQL.

 

To set the name of the output file dynamically in a data step use the FILEVAR= option on the FILE statement.  If you want comma delimited values use the DSD option.  If you want to force quotes around values that don't need them then use the ~ modifier on the PUT statement.

 

So if your &BENPK and &COMP macro variables are just the values of the BENPK and COMP dataset variables then your code can be like this.  

data _null_;
  set &outdat.;
  length filename $256 ;
  filename=cats("&drv2./test/Current/",benpk,'_',comp,'_00.txt');
  file out filevar=filename dsd mod;
  put (name fname zip) (~) ;
run;

If they aren't dataset variables the instead of BENPK and COMP in the list of values for CATS() to concatenate then use "&BENPK" and "&COMP".  Or use SYMGET('BENPK') and SYMGET('COMP').

cho16
Obsidian | Level 7
Thank You everyone !

i have used the below code to rename the external file.

data _null_;
RC1=rename("&drv2./test/Current/C.txt", "&drv2./test/Current/C.txt", "file");
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1000 views
  • 2 likes
  • 3 in conversation