BookmarkSubscribeRSS Feed
EC27556
Quartz | Level 8

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;
4 REPLIES 4
Tom
Super User Tom
Super User

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|

 

 

EC27556
Quartz | Level 8

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!

Tom
Super User Tom
Super User

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.

ballardw
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1275 views
  • 1 like
  • 3 in conversation