1
votes

I am trying to concatenate some variables in SAS Enterprise guide.

I start with a SAS_table, which looks something like (note the empty column plums and the empty observation for pears:

id      farm         apples        pears           cherries          plums
1       'Munich'     'Granny'      'Williams'      'RedDelight'
2       'Amsterdam'  'PinkLady'                    'Scrumptious
3       'Moscow'     'Braeburn'    'Williams'      'RedDelight'     

Then I create global variables as follows:

%do i = 1 %to 3; 
    %global farm_&i., apples_&i., pears_&i., cherries_&i., plums_&i.;
%end; 

And I store the above table in the global variables as follows:

data _NULL_; 
    set SAS_table; 
    %do i = 1 %to 3; 
        if id = &i. then do; 
            call symput("farm_&i.", farm); 
            call symput("apples_&i.", apples); 
            call symput("pears_&i.", pears); 
            call symput("cherries_&i.", cherries); 
            call symput("plums_&i.", plums); 
        end; 
    %end;
run; 

All of the above seems to work smoothly. In a next step I want to the individual variables to get the following output (please note that empty values are just skipped - no extra delimiter (" :: ") is set for these cases):

concat_1:      Munich :: Granny :: Williams :: RedDelight
concat_2:      Amsterdam :: PinkLady :: Scrumptious
concat_3:      Moscow :: Braeburn :: Williams :: RedDelight

To do this, I use the following code:

data _NULL_; 
    %do i = 1 %to 3;
         call_symput("concat_&i",catx(" :: ", &&farm_&i., &&apples_&i.., &&pears_&i.., 
             &&cherries_&i.., &&plums_&i..));
    %end;
run; 

However, when I run this code I get the error:

ERROR 159-185: Null parameters for CATX are invalid.

My assumption here is that SAS is unhappy because some of the variables can be empty values. But how do I fix this? I've messed around loads and just can't seem to find a good answer.

1
In your CATX() You need to enclose the macro variables in quotes. Right now it's looking for variable names that are ALL null which is causing the issue.Reeza
Ps catx('::', of farms--plums); should work from the start.Reeza
Why are you working through all of this mess in macro variables? Use data step.Joe
@Joe: Because the client said so @Reeza: catx('::', of farms--plums); works for me, but enclosing the variables didn't end up working. This works as a quick fix though, so thanks :-)MRR

1 Answers

1
votes

It looks to me like your macro variables will have values like Munich in them. So the SAS code that your macro is generating needs to generate a string literal like "Munich" otherwise SAS will look for a variable with that name. This also explains the error about null values since without the quotes to make the null value into a string literal you end up with a CATX() call with adjacent delimiters.

data _NULL_; 
%do i = 1 %to 3;
  call_symput("concat_&i",catx(" :: ", "&&farm_&i.", "&&apples_&i.."
             , "&&pears_&i..", "&&cherries_&i..", "&&plums_&i.."))
  ;
%end;
run; 

You could instead generate the macro variables with the quotes, but they would probably be much less useful as macro variables in that form. So instead of changing the last call symput() statement you could change the earlier ones that make the macro variables. You could also remove the unneeded macro code from that step.

data _NULL_; 
  set SAS_table; 
  call symput(cats("farm_",id), quote(farm)); 
  call symput(cats("apples_",id), quote(apples)); 
  call symput(cats("pears_",id), quote(pears)); 
  call symput(cats("cherries_",id), quote(cherries)); 
  call symput(cats("plums_",id), quote(plums)); 
run;