Search code examples
variablessassas-macrodatastep

SAS variable concatenation through data step


I am looking for a way to create a string variable containing certain values of the dataset while going through the data step.

Example data set work.test:

AddToStringYN    Value
     Y           One
     Y           Two
     N           Three
     Y           Four
     N           Five

So in the end, the variable would look like: OneTwoFour (or even better FourTwoOne). This looks so simple, but I can't seem to find a way to do it. I also tried to work with macro variables like this:

%let stringvar=;
Data _null_;
  set work.test;
  if AddToStringYN = "Y" then do;
    call symput('stringvar',"&stringvar" || strip(value));
  end;
Run;

But this gives:

GLOBAL STRINGVAR Four

So I only get the last value. I get that this must be because of some misunderstanding of mine about this macro facility, but I don't understand why there is only the last value in the variable. I thought it was only the last time the symput was called that it was actually executed or something, but then when I adjust the code to:

%let stringvar=;
Data _null_;
  set work.test;
  if AddToStringYN = "Y" then do;
    call symput('stringvar'||strip(value),"&stringvar" || strip(value));
  end;
Run;

Then I do get them all:

GLOBAL STRINGVARONE  One
GLOBAL STRINGVARTWO  Two
GLOBAL STRINGVARFOUR  Four

So my last guess is that going through the data step, the 'call symput...' line is actually added to the macro processor where the "&stringvar" is already replaced and only after the final statement are they all executed.
Is this a good assumption or is there another explanation? And back to the original question: is there an easy way to achieve this (having the desired variable)?


Solution

  • Greetings Seems simple enough, here is my solution:

    data a;
    set test end=eof;
    length cat $100.;
    retain cat;
    if AddToStringYN = "Y" then do;
       cat=trim(left(cat))||trim(left(value));
    end;
    if eof then do;
       call symput("VAR",cat);
       output;
    end;
    run;
    
    %put VAR=&VAR;
    

    in this example you have the concatenation of your variable in the A dataset in the column "CAT" and you have a macrovariable VAR with the same list