Macro variable question (leading spaces)

Accepted Solution Solved
Reply
Contributor AD
Contributor
Posts: 31
Accepted Solution

Macro variable question (leading spaces)

If I do this, it works:

%let var = 60;

Proc Freq data = Data1;

  Table Item&var;

Run;

But if I create the "var" macrovariable using a call Symput statement (see below) it doesn't work. 

Data test;

  number = 60.111;

run;

Data null; set test;

  call symput ("Var",Round(number,1));

run;

Proc Freq Data=Data1;

  Table Item&var;

Run;

I get the following error in the log:

NOTE: Line generated by the macro variable "VAR".

19          Item          60

                          __

                          22

                          200

ERROR: Variable ITEM not found.

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, *, -, /, :, ALL,

              CHARACTER, CHAR, NUMERIC

ERROR 200-322: The symbol is not recognized and will be ignored.

It looks like it is putting a bunch of leading spaces in front of the 60... any ideas?


Accepted Solutions
Solution
‎06-22-2013 08:10 PM
PROC Star
Posts: 1,322

Re: Macro variable question (leading spaces)

Hi,

Yep, you've diagnosed it correctly.

If you want to prove it to yourself, try:

%put >>&var.<<;

And you will see the spaces.

In the dark ages you might need to add  trim((left()) inside your CALL SYMPUT.   But in these enlightened times, try changing to Call SymputX.  It will left justify the value and trim trailing spaces automatically.

HTH,

--Q.

View solution in original post


All Replies
Solution
‎06-22-2013 08:10 PM
PROC Star
Posts: 1,322

Re: Macro variable question (leading spaces)

Hi,

Yep, you've diagnosed it correctly.

If you want to prove it to yourself, try:

%put >>&var.<<;

And you will see the spaces.

In the dark ages you might need to add  trim((left()) inside your CALL SYMPUT.   But in these enlightened times, try changing to Call SymputX.  It will left justify the value and trim trailing spaces automatically.

HTH,

--Q.

Super Contributor
Posts: 644

Re: Macro variable question (leading spaces)

A quick fix is

    %let var = &var ;


Yup, it strips leading and trailing spaces

I believe using symputx will also fix the problem

also you could use

     Table Item%left(&var) ;

Richard

Contributor AD
Contributor
Posts: 31

Re: Macro variable question (leading spaces)

Thanks to you both!  SymputX works beautifully.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 195 views
  • 3 likes
  • 3 in conversation