Concatenate strings in macro with logical operators

Reply
N/A
Posts: 1

Concatenate strings in macro with logical operators

I am trying ot make a macro loop that recrates a something like this:

DATA newDataSet;

     SET oldDataSet;

     d_oldVar1=(oldVar1>0);

     d_oldVar2=(oldVar2>0);

     d_oldVarx=(oldVarx>0);

RUN;

This is what I have so far:

%macro variable_length;
  %do i=1 %to %eval(/*&VarListCount*/ 3);
        %IF &i = 1 %THEN %DO;

            %LET var_gt_test = d_%scan(&VarNameList,%eval(&i))%str(=()%scan(&VarNameList,%eval(&i))%STR(>0))Smiley Wink ;

      %END;
      %ELSE %DO;

            %LET temp_var_gt_test = d_%scan(&VarNameList,%eval(&i))%str(=()%scan(&VarNameList,%eval(&i))%STR(>0)Smiley Wink) ;

      %END;
  %END;

%mend variable_length;

this issue is trying to concatenate stings with ";" or ")" in them.

EDIT:

I found an answer to the problem by using &&:

semic=%STR(Smiley Wink;


%LET lftCln = (;


%LET rgtCln = );

temp_var_gt_test = d_%scan(&VarNameList,%eval(&i))%str(=&&lftCln.)%scan(&VarNameList,%eval(&i))%STR(>0&&rgtCln.&&semic.) ;


%LET var_gt_test = &var_gt_test%STR( )&temp_var_gt_test;

If you have a diffent way please post it.

Super User
Posts: 5,080

Re: Concatenate strings in macro with logical operators

Two other possibilities ...

First, why concatenate them all?  Why not just code along these lines:

%do i=1 %to 3;

   %local next_var;

   %let next_var = %scan(&VarNameList, &i);

   d_&next_var = (&next_var > 0);

%end;

And a second possibility would be to construct your long string in a DATA step, using CALL SYMPUT to capture the final value.

Good luck.

Ask a Question
Discussion stats
  • 1 reply
  • 216 views
  • 0 likes
  • 2 in conversation