BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

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

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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;

 

View solution in original post

7 REPLIES 7
Reeza
Super User

TRIMMED option

 

hhchenfx
Rhodochrosite | Level 12

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.

Reeza
Super User

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. 

data_null__
Jade | Level 19
numeric to character conversion. right justified in a field 12 bytes long
hhchenfx
Rhodochrosite | Level 12

So we can do Trim inside the SQL?

SASKiwi
PROC Star

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;

 

Ksharp
Super User
proc sql noprint; 
 select RR into: RR   separated by ' '
 from w;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 5024 views
  • 2 likes
  • 5 in conversation