remove trailing spaces inside SAS macro - Stack Overflow

admin2025-04-29  0

I want to remove the trailing spaces on variable inside the macro, i have tried TRIM, STRIP, %LET nothing is working. Can I get help on this please?

%MACRO DRY(count);
data _NULL_;
inpcount = "&count";
baseref1 = substr(jobid,4,2);   ------ value of the jobid is 75412
if inpcount < 10 then do;
   baseref = baseref2 || inpcount ;
end;
else do;
   baseref = baseref2 || inpcount ;
end;
call symputx('baseref',baseref,'g');
run;

%put baseref: &baseref;

--- I am getting the value as "SYMBOLGEN: Macro variable BASEREF

resolves to 38   1
baseref: 38   01" 

Expected result should be baseref: 3801

I want to remove the trailing spaces on variable inside the macro, i have tried TRIM, STRIP, %LET nothing is working. Can I get help on this please?

%MACRO DRY(count);
data _NULL_;
inpcount = "&count";
baseref1 = substr(jobid,4,2);   ------ value of the jobid is 75412
if inpcount < 10 then do;
   baseref = baseref2 || inpcount ;
end;
else do;
   baseref = baseref2 || inpcount ;
end;
call symputx('baseref',baseref,'g');
run;

%put baseref: &baseref;

--- I am getting the value as "SYMBOLGEN: Macro variable BASEREF

resolves to 38   1
baseref: 38   01" 

Expected result should be baseref: 3801

Share Improve this question edited Jan 7 at 2:45 Tom 51.8k2 gold badges18 silver badges35 bronze badges asked Jan 7 at 1:58 samratsamrat 412 silver badges8 bronze badges 1
  • The code in the question makes me concerned for whoever has to maintain it. – Richard Commented Jan 7 at 10:20
Add a comment  | 

1 Answer 1

Reset to default 1

There is nothing wrong with your MACRO variable.

The mistake is in the SAS code the macro generates, your use of the || operator. Use the cats() function instead.

baseref = cats(baseref2,inpcount) ;

Also you need to decide if INPCOUNT should be a character variable or a numeric variable.

You create it as character.

inpcount = "&count";

But then compare it to a number.

if inpcount < 10 then do;

If you want to convert a number to a string with leading zeros use the Z format. For example to always generate two digits even when the number INPCOUNT is less than 10 use the Z2. format.

baseref = cats(baseref2,put(inpcount,z2.)) ;
转载请注明原文地址:http://anycun.com/QandA/1745934755a91334.html