BookmarkSubscribeRSS Feed
sam1231
Obsidian | Level 7

Hi,

 

%put pd=sit-01\sdf\rtm\tbg\pdn\tl\output

 

%let pth=&pd,&pd1,

 

%put pth = sit-01\sdf\rtm\tbg\pdn\tl\output              ,

 

How can i remove trailing blank in %let variable ? 

can i remove trailing blank in %let statement?

 

Thanks

13 REPLIES 13
Jagadishkatam
Amethyst | Level 16

You can use the %trim

 

%let pth=%trim(&pd,&pd1);
Thanks,
Jag
sam1231
Obsidian | Level 7
its not working! it says
ERROR: More positional parameters found than defined.
DrAbhijeetSafai
Pyrite | Level 9

This did not help me directly, because that was not my question, but this answer helped me indirectly to solve another problem. Thanks.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Associate Data Analyst
Actu-Real
Astounding
PROC Star
You can remove trailing blanks with a %let statement:

%let pd=&pd;
%let pd1=&pd1;
%let pth=&pd,&pd1, ;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @sam1231 

 

If you create a macro variable with a %let-statement, you never get leading or trailing blanks.

 

It only happens if you create the macro variable in a data step or proc sql. Note that you get leading blanks if the origin is a numeric variable and trailing blanks if it is a character variable.

 

Here is a bit of code you can try to explore the different ways of creating macro variables with or without blanks. Note the different behaviour in examples 3 and 6, where one would expect the same to happen.

 

/* Creation of macro variables with and without trailing blanks */

* Example 1 - Macro assignment - never with trailing blanks;
%let var1 =        first                 ;
%let var2 =        second                ;
%let var3 =        5                     ;
%put &var1*&var2*&var3*;

/* From existing data - first make some test data with trailing blanks */
data test;
	length var1 var2 $10;
	var1 = 'first';
	var2 = 'second';
	var3 = 5;
run;

* Example 2 - Data step - symput - blanks preserved;
data _null_; set test;
	call symput('var1',var1);
	call symput('var2',var2);
	call symput('var3',put(var3,8.));
run;
%put &var1*&var2*&var3*;

* Example 3 - Data step - symput of trimmed/stripped variables - without blanks;
data _null_; set test;
	call symput('var1',trim(var1));
	call symput('var2',trim(var2));
	call symput('var3',strip(put(var3,8.)));
run;
%put &var1*&var2*&var3*;

* Example 4 - Data step - symputx - without blanks;
data _null_; set test;
	call symputx('var1',var1);
	call symputx('var2',var2);
	call symputx('var3',var3);
run;
%put &var1*&var2*&var3*;

* Example 5 - Proc sql - select variables - blanks preserved;
proc sql noprint;
	select var1, var2, var3 into :var1, :var2, :var3
	from test;
quit;
%put &var1*&var2*&var3*;

* Example 6 - Proc sql - Select trimmed variables - DOES NOT WORK - still with blanks;
proc sql noprint;
	select trim(var1), trim(var2), strip(put(var3,8.)) into :var1, :var2, :var3
	from test;
quit;
%put &var1*&var2*&var3*;
	
/* Removing leading/trailing blanks from existing macro variables */

* Create test variables with blanks;
data _null_; set test;
	call symput('var1',var1);
	call symput('var2',var2);
	call symput('var3',var3);
run;
%put &var1*&var2*&var3*;

* Example 7 - Remove with macro assignment;
%let var1 = &var1;
%let var2 = &var2;
%let var3 = &var3;
%put &var1*&var2*&var3*;

* Create test variables with blanks;
data _null_; set test;
	call symput('var1',var1);
	call symput('var2',var2);
	call symput('var3',var3);
run;
%put &var1*&var2*&var3*;

* Example 8 - Remove with %trim;
%let var1 = %trim(&var1);
%let var2 = %trim(&var2);
%let var3 = %trim(&var3);
%put &var1*&var2*&var3*;
Tom
Super User Tom
Super User

You left out an SQL example using the TRIMMED keyword.

* Example 5X - Proc sql - select variables - blanks removed;
proc sql noprint;
  select var1, var2, var3
    into :var1 trimmed, :var2 trimmed, :var3 trimmed
    from test
  ;
quit;
%put *&var1*&var2*&var3*;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Tom 

 

Thank you. I wasn't aware of the TRIMMED option. Actually, when I made the examples, it puzzled me that select into didn't trim by defaul. as I expected, when the option NOTRIM wasn't specified.

 

 

 

Tom
Super User Tom
Super User

The NOTRIM is only used to prevent the normal trimming done when pulling MULTIPLE values (ie using range of macro variables or SEPARATED BY keyword to concatenate values).  It does not apply to queries that pull just one value.

 

Before they created the TRIMMED keyword the trick to get SQL to trim a value placed in a macro variable was to use the SEPARATED BY keyword even when the query could only produce one observation.

sam1231
Obsidian | Level 7

my macro variable value is 

%put pth = sit-01\sdf\rtm\tbg\pdn\tl\output              ,

 

you can see there is space between  output  and (,). how to remove that?

 

Basically i have 8 macro variables created like &pd &pd1 &pd2 &pd3 ....... &pd8

 

I am trying to create list of all 8 macro variables and they must be separated by (,)

 

so i am using %let pth=&pd,&pd1,&pd3.......,&pd8;

 

it will add delimiter but add space too as i shown in %put statement.

 

How can i remove that space? 

Astounding
PROC Star

The easiest way to correct it would be to revise the code that actually creates the macro variables with those extra blanks.  But you would have to post what that code looks like to get a solution.

 

Other solutions are possible, but more complex because they have to check:

 

  • What if the last character for one of the variables is not actually a comma?
  • What if there are additional blanks inside a macro variable value that should not be removed?
sam1231
Obsidian | Level 7
i am trying to apply delimiter(,) between macro variables
Tom
Super User Tom
Super User

@sam1231 wrote:

my macro variable value is 

%put pth = sit-01\sdf\rtm\tbg\pdn\tl\output              ,

 

you can see there is space between  output  and (,). how to remove that?

 

Basically i have 8 macro variables created like &pd &pd1 &pd2 &pd3 ....... &pd8

 

I am trying to create list of all 8 macro variables and they must be separated by (,)

 

so i am using %let pth=&pd,&pd1,&pd3.......,&pd8;

 

it will add delimiter but add space too as i shown in %put statement.

 

How can i remove that space? 


First show how you created the macro variables with the extra spaces on the end.

Do the macro variables contain macro quoting?

Here is brute force way to remove the trailing spaces.

First let's make some macro variables that contain trailing spaces.  Note that it is actually hard to make macro variables that actually contain trailing spaces, unless you use SAS code, and even then you need to use poor coding techniques. Like using the older CALL SYMPUT() function instead of the newer CALL SYMPUTX() function.

data _null_;
  call symput('pd','5spaces     ');
  call symput('pd2','3spaces   ');
  call symput('pd3','nospaces');
  call symput('pd4','2spaces  ');
run;
279   %put |&pd,&pd2,&pd3,&pd4|;
|5spaces     ,3spaces   ,nospaces,2spaces  |

If the spaces are like this example and do not have any macro quoting applied then a simple %LET statement will remove the trailing spaces. So build up your path in pieces.

280   %let pth=&pd;
281   %let pth=&pth,&pd2;
282   %let pth=&pth,&pd3;
283   %let pth=&pth,&pd4;
284   %put |&pth|;
|5spaces,3spaces,nospaces,2spaces|

Or you could use SAS code instead.

285   data _null_;
286     call symputX('pth',catx(',',"&pd","&pd2","&pd3","&pd4"));
287   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


288   %put |&pth|;
|5spaces,3spaces,nospaces,2spaces|

 

ballardw
Super User

@sam1231 wrote:

Hi,

 

%put pd=sit-01\sdf\rtm\tbg\pdn\tl\output

 

%let pth=&pd,&pd1,

 

%put pth = sit-01\sdf\rtm\tbg\pdn\tl\output              ,

 

How can i remove trailing blank in %let variable ? 

can i remove trailing blank in %let statement?

 

Thanks


Since you did not END the %let statement properly it is extremely hard to tell what you have. SAS use semicolons the ; to end statements. You don't show a single : in the above. So where do YOU think the "spaces" are coming from?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 13 replies
  • 22414 views
  • 12 likes
  • 7 in conversation