Search code examples
sassas-macro

why macro is creating Leading space while resolving macro in sas?


I am submitting the following SAS code:

 proc format;
   picture mysdt
   low-high = '%Y%0m%0d%0H%0M' (datatype =datetime);
  run;

 DATA _NULL_;
   call symput("Today", Put(datetime(),mysdt.));
 run;

 %put t_&today;

The resulting log shows 2 spaces before the datetime:

t_  201504240150

The problem here is when my macro is resolved it is creating leading space. Why is it creating spaces?

My output should be:

t_201504240150

I know the solution but just wanted to know the reason.

DATA _NULL_;
  call symput("Today", strip(Put(datetime(),mysdt.)));
run;

Solution

  • The reason for this is that your format is set up with a default length of 14. Therefore, when you go to put your value into &today it is stored with leading blanks to fill out the length to 14. From the SAS documentation:

    DEFAULT=length

    specifies the default length of the picture. The value for DEFAULT= becomes the length of picture if you do not give a specific length when you associate the format with a variable.

    So, there are a number of options:

    Set the default format length to match the expected length of your datetime value (using DEFAULT=12):

    proc format;
      picture mysdt (default=12)
      low-high = '%Y%0m%0d%0H%0M' (datatype =datetime);
    run;
    

    Specify the width in your format:

    DATA _NULL_;
      call symput("Today", strip(Put(datetime(),mysdt12.)));
    run;
    

    Or, as previously answered, use call symputx to trim whitespace:

    DATA _NULL_;
      call symputx("Today", strip(Put(datetime(),mysdt.)));
    run;
    

    Personally, I'd fix the format to default to 12, then you won't need to remember to specify width or use call symputx each time.