Hi Everyone,
I use SQL to save macro value.
Some how when I use it in code, the value has space in front which return error.
proc sql noprint; select RR into: RR from w; quit;
%put "macro value:" &rr
...
keep type_RR&RR._sl
when &RR=3, the above code return these value. there is space in front.
In SAS log, that space appear to be a "tab" (big gap).
"macro value:" 3
Type_RR 3_sl
Can you please help me with that?
Thank you,
HHCFX
Yes, like this:
proc sql noprint;
select age into :age trimmed
from sashelp.class
where name = 'Mary'
;
quit;
%put age = &age;
Or you can use SQL functions to get the same result:
proc sql noprint;
select strip(put(age, 8.)) into :age
from sashelp.class
where name = 'Mary'
;
quit;
TRIMMED option
My trick is:
after the SQL to get macro variable RR, I put the code
%let RR=&RR;
so the problem is solved.
But so curious why that space come in the first place.
That's an extra step and a really old way to deal with it.
@hhchenfx wrote:
My trick is:
after the SQL to get macro variable RR, I put the code
%let RR=&RR;
so the problem is solved.
But so curious why that space come in the first place.
It's because macro variables are characters and you didn't specify a length so it assumed one and added spaces for it.
So we can do Trim inside the SQL?
Yes, like this:
proc sql noprint;
select age into :age trimmed
from sashelp.class
where name = 'Mary'
;
quit;
%put age = &age;
Or you can use SQL functions to get the same result:
proc sql noprint;
select strip(put(age, 8.)) into :age
from sashelp.class
where name = 'Mary'
;
quit;
proc sql noprint;
select RR into: RR separated by ' '
from w;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.