Add To Item

From sasCommunity
Jump to: navigation, search

Macro Add2Item

Given a list of items separated by spaces, this macro will enclose each item with left and right special characters and separate each item with a delimiter.

 /*       name:  Add2Item.sas
specifications:  description: returns items in List
                              surrounded by &Left and &Right
                                                         default: sQuote
                              delimited  by &Separated_By default: comma
                     purpose: provide tool to add delimiters
                              and quotes to by-var list

------------------------------------------------------------------------
contexts     : program group: development tool
               program  type: subroutine
                   SAS  type: macro function
------------------------------------------------------------------------
specifications: input  : list of tokens
                         enclosure   : tokens for left and right
                         separated by: token
                process: read item from list
                         concatenate mVars of enclosure and separator
                output : returns string
                note   : used by macro Invalid
------------------------------------------------------------------------
usage:
%Let ListNew = %Add2Item(List     = A B C
                        ,Left     = x
                        ,Right    = y
                        ,separated_by = Z);
%Let IdList = C D ;
%Put where Name in (%Add2Item(List=&IdList.));
%Let by_vars = VarA;
%Put tables %Add2Item(List=&By_Vars. VarC,left=,right=
            ,separated_by =*);
------------------------------------------------------------------------
License type: Free, LaTeX project public license
              http://www.latex-project.org/lppl/
Author: Ronald J. Fehd ************************/
%MACRO Add2Item/* ---------------------- ------------- */
        (List      =          /* of items              */
        ,Left      =%str(%')  /* sQuote                */
        ,Right     =%str(%')  /* sQuote                */
        ,Delimiter =%str( )   /* of list: space        */
        ,Separated_by=%str(,) /* output: default:comma */
)/des =
"returns items in list surrounded and separated_by"
 /* ** store source **/
;/*RJF2 04Jan24 developed at home
        suggest other names: enclose surround
RJF2 04Jan27 added unquote
RJF2 04Mar12 polishing test suite
RJF2 04May03 polishing for testsuite
*** .................................... */
%local I     ;          %*local macro loop counter/index*;
%local Item  ;          %*local temp var for Qscan      *;
%local Return;          %*return value                  *;
                        %let I      = 1;
                        %let Item   = %Qscan(&List,&I,&Delimiter);
%do %until(&Item. = );
    %if &Item. ne %then %let Return = &Return.&Left.&Item.&Right;
                        %let I      = %eval(&I + 1);
                        %let Item   = %Qscan(&List,&I,&Delimiter);
    %if &Item. ne %then %let Return = &Return.&Separated_By;
    %end;%unquote(&Return.)%Mend;
 /*\begin{testSuite:macro} ************* **
%Let ListNew = %Add2Item(List     = A B C
                        ,Left     = x
                        ,Right    = y
                        ,separated_by = Z);
%Put ListNew<&ListNew.>;*xAyZxByZxCy;
%Let IdList = C D ;
%Put where Name in (%Add2Item(List=&IdList.));
%Let By_Vars = VarA;
%Put tables %Add2Item(List = &By_Vars. VarC
                     ,left=,right=
                     ,separated_by = *);
run; /*\end{testSuite:macro} ********** */

--macro maven == the radical programmer 14:17, 21 August 2007 (EDT)