See the below, I have a macro that exists that equals "1". Therefore, when I run the below I expect to see test="alpha1" in the created dataset. Instead I get "alpha 1". Why is this, and is there any way to get rid of these blank spaces between alpha and 1?
data test; test="alpha&test1"; run;
Check what value it actually has by running something like:
%put |&test1|;
One easy way to remove them is it just use a %LET to recreate the macro varaible.
%let test1=&test1;
You did not show how you created TEST1.
Here are two mistakes users can make that lead to leading spaces in macro variables.
1) Using the ancient CALL SYMPUT() function instead of the modern CALL SYMPUTX().
2) Using the INTO clause in SQL without using the TRIMMED keyword.
data test;
num=1;
string=' 1';
call symput('bad1',num);
call symputx('good1',num);
call symput('bad2',string);
call symputx('good2',string);
run;
proc sql noprint;
select num,num,string,string
into :bad3
, :good3 trimmed
, :bad4
, :good4 trimmed
from test
;
quit;
604 %put |&good1| |&bad1| ; |1| | 1| 605 %put |&good2| |&bad2| ; |1| | 1| 606 %put |&good3| |&bad3| ; |1| | 1| 607 %put |&good4| |&bad4| ; |1| | 1|
Thanks,
I actually created a range of 'test' macros using the old symput from the TEST.TEST data table:
data _NULL_; set Test.Test nobs=TEST_TOT; call symput ('Test'||left(put(_n_,3.)),test); call symput ('TEST_TOT',TEST_TOT); run;
if i were to use symputx would the test variables resolve correctly then?
Have also worked out i can put in a %trim function to fix too!
Use modern techniques.
data _NULL_;
if eof then call symputx('TEST_TOT',TEST_TOT);
set Test.Test end=eof nobs=TEST_TOT;
call symputx(cats('Test',_n_),test);
run;
But I always wonder what value is gained by extracting data from datasets and stuffing it into so many macro variables.
If you want to use it generate code then use the data step to generate the code and leave the data in the dataset.
@EC27556 wrote:
Thanks,
I actually created a range of 'test' macros using the old symput from the TEST.TEST data table:
data _NULL_; set Test.Test nobs=TEST_TOT; call symput ('Test'||left(put(_n_,3.)),test); call symput ('TEST_TOT',TEST_TOT); run;if i were to use symputx would the test variables resolve correctly then?
Have also worked out i can put in a %trim function to fix too!
TEST IT!
It is so hard to do:
data test; test=4; run; data _null_; set test; call symput ('Test1',test); call symputx ('Test2',test); run; %put test1 is:|&test1.|; %put test2 is:|&test2.|;
Note: without a very clear definition of "resolve correctly" I won't say that it will.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.