BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
obvisou
Fluorite | Level 6

Hi all,
I would like to add a new value in an existing Macro-Variable. The new value should only be added if is not already part of the Macro-Variable.
Adding a value works fine:

%let parameter_macrovariable = &parameter_macrovariable. &parameter_value.;

To do the check I would like to use a Macro (if there is not a better way to do it). I haven't find a solution to work inside the Macro with the literal name of Inputparamter in the macro (parameter_macrovariable 😃 The assignment inside a macro is different. This is in SAS not allowed: %let %parameter_macrovariable. = ...

Thank you for your help. Here my not working macro:

%global macrovariable
%let macrovariable = Value1

%macro check_and_add(parameter_macrovariable =, parameter_value =);

    %let found = %sysfunc(findw(&parameter_macrovariable., &parameter_value., %str( )));
    %if &found. = 0 %then %do;
        %let parameter_macrovariable = &parameter_macrovariable. &parameter_value.;
		%put Updated list: &parameter_macrovariable., new added value: &parameter_value.;
    %end;

%mend;

%check_and_add(parameter_macrovariable = macrovariable, parameter_value = Value2)

%put parameter_macrovariable: parameter_macrovariable; /* Result: empty */
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@obvisou wrote:

This Code gives this ERROR: Invalid Macro-Variable is already used by chkvaluelist_Add. ERROR 180-322: Statement is not valid or it is used out of proper order:

%global parameter_macrovariable;
%let parameter_macrovariable  =;

%MACRO ChkValueList_Add(p_list =, p_value =);
%if %sysfunc(findw(&p_list., %p_value.,,s)) %then %do;
	&p_list.;
%end;
%else %do;
	&p_list. &p_value.;
%end;
%MEND;
ChkValueList_Add(p_list = &parameter_macrovariable .  ,p_value = Value1);

Remove the extra semicolons from these line:

...
	&p_list.;
..
	&p_list. &p_value.;

You do NOT want those to be part of the text that the macro emits.

 

Make sure to use the % when calling the macro. Without that CHKVALUELIST_ADD is ignored by the macro processor and so will cause a syntax error by SAS itself.

 

Finally call the macro in a place where a single string is expected by SAS.  

Such as in an %LET macro statement.

%let parameter_macrovariable = %ChkValueList_Add(p_list = &parameter_macrovariable .  ,p_value = Value1);

So if you set 

%let parameter_macrovariable = ABC ;

And then run :

%let parameter_macrovariable = %ChkValueList_Add(p_list = &parameter_macrovariable .  ,p_value = Value1);

You will get 

ABC Value1

Assigned to parameter_macrovariable .

View solution in original post

32 REPLIES 32
rudfaden
Lapis Lazuli | Level 10

when you use a variable in as a parameter in a macro it is definled in the local macro variable table and is not availble outside the macro. You need do define it before.

 

%let macrovariable = Value1;
%let parameter_macrovariable = &macrovariable;

%macro check_and_add( parameter_value =);
    %let found = %sysfunc(findw(&parameter_macrovariable., &parameter_value.));
    %if &found. = 0 %then %do;
        %let parameter_macrovariable = &parameter_macrovariable. &parameter_value.;
		%put Updated list: &parameter_macrovariable., new added value: &parameter_value.;
    %end;
%put &=parameter_macrovariable; 
%mend;

%check_and_add(parameter_value = Value2)

%put &=parameter_macrovariable; 
obvisou
Fluorite | Level 6

Thank you. But this is not want I want. The Macro should be dynamic and be used also for other Macro-Variables, so it is necessary to have two Input-Parameters. One for the Macro-Variable (List) to be updated and second for the Value who must be added or not (parameter_macrovariable =, parameter_value 😃

yabwon
Onyx | Level 15

Try this:

%global myMacrovariable;
%let myMacrovariable = A;

%put &=myMacrovariable.;

%macro add_value(val,mvName=myMacrovariable)/minoperator mindelimiter=' ';
%if NOT (&val. in (&&&mvName.)) %then
  %let &mvName. = &&&mvName. &val.;
%mend add_value;

%add_value(A)
%put &=myMacrovariable.;

%add_value(B)
%put &=myMacrovariable.;

%add_value(C)
%put &=myMacrovariable.;

%add_value(C)
%put &=myMacrovariable.;

%add_value(A)
%put &=myMacrovariable.;

Of course the assumption is that values you're adding don't have spaces in them.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

An alternative could be to add everything as it goes to the variable value, and when it's done, to use the %dedupListS() macro from the BasePlus package:

 %let list = 4 5 6 1 2 3 1 2 3 4 5 6;
 %let list = %dedupListS(&list.);

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



obvisou
Fluorite | Level 6

Thanks Bard.

Had to change the if statement to:

%let pos_found = %sysfunc(findw(&mvName., &val.));

%if &pos_found = 0 %then % do:

 

But this is still not allowed:

%let &mvName. = &&&mvName.   &val.;

 

ERROR: Expecting a variable name after %let.

ERROR: The macro variable name is either all blank or missing.

yabwon
Onyx | Level 15

Full log please...

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

"ERROR: The macro variable name is either all blank or missing." - did you defined your macro variable BEFORE running the macro ?

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



obvisou
Fluorite | Level 6
%MACRO ChkValueList_Add(p_list     =                            
                           ,p_value    =                            
                           )/minoperator mindelimiter = ' ';


/*----------------------------------------------------------------
  Parameter
----------------------------------------------------------------*/
%put @_&sysmacroname._500_p_list:          &p_list.;
%put @_&sysmacroname._502_p_value:         &p_value.;


/*----------------------------------------------------------------
  Check
----------------------------------------------------------------*/
%if %length(&p_value.) = 0 %then %do;
   %put ERROR: Parameter 'p_value' ist leer. (ku-508);
   %return;
%end;


/*----------------------------------------------------------------
  
----------------------------------------------------------------*/
%let pos_found = %sysfunc(findw(&p_list., &p_value.));           /* */ 
/*%if NOT (&p_value. IN (&&&p_list.)) %then %do;                 /* */ 
   %put @_&sysmacroname._517_Position_Found:  &pos_found.;

%if &pos_found. = 0 %then %do;               
   %let &p_list. = &&&p_list. &p_value.;     

   %put @_&sysmacroname._522:  Wert (&p_value.) wurde in Liste hinzugefügt (&p_list.).;
%end;
%else %do;
   %put @_&sysmacroname._525:  Wert (&p_value.) ist bereits in Liste enthalten (&p_list.).;		/* nicht möglich Name der Liste anzuzeigen!!!)
%end;

%put;
%put #---------------------------------------------------#;
%put # &sysmacroname._End;
%put #---------------------------------------------------#;

%MEND ChkValueList_Add;



------------------------------------------------------------------------------
 
------------------------------------------------------------------------------
%global list_epi list_add;
%let list_epi               =;                                       
%let list_add               =;                                       
/* */

%include "/......_ChkVariableExist.sas";    
%ChkValueList_Add(p_list= &list_epi., p_value= Hallo); 
%put ----list_epi:  &list_epi.;


------------------------------------------------------------------------------
 Log
------------------------------------------------------------------------------
1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Config';
4          %LET _CLIENTPROCESSFLOWNAME='Manual';
5          %LET _CLIENTPROJECTPATH='/SAS/......egp';
6          %LET _CLIENTPROJECTPATHHOST='........';
7          %LET _CLIENTPROJECTNAME='.......egp';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=SVG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         %macro HTML5AccessibleGraphSupported;
15             %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16         %mend;
17         FILENAME EGHTML TEMP;
18         ODS HTML5(ID=EGHTML) FILE=EGHTML
19             OPTIONS(BITMAP_MODE='INLINE')
20             %HTML5AccessibleGraphSupported
MPRINT(HTML5ACCESSIBLEGRAPHSUPPORTED):   ACCESSIBLE_GRAPH
21             ENCODING='utf-8'
22             STYLE=HTMLBlue
23             NOGTITLE
24             NOGFOOTNOTE
25             GPATH=&sasworklocation
26         ;
27         
28         
29         %include "/SAS/................sas";
574        %ChkValueList_Add(p_list= &list_epi., p_value= Hallo);


#---------------------------------------------------#
# ChkValueList_Add_Start
#---------------------------------------------------#
@_ChkValueList_Add_500_p_list:
ERROR: Expecting a variable name after %LET.
ERROR: The macro variable name is either all blank or missing.
ERROR: The macro ChkValueList_Add will stop executing.
575        
576    
578        
579        %put ----list_epi:  &list_epi.;
----list_epi:
580        %put &=list_epi;
LIST_EPI=
581        /* */
582        
583        
584        %LET _CLIENTTASKLABEL=;
585        %LET _CLIENTPROCESSFLOWNAME=;
586        %LET _CLIENTPROJECTPATH=;
587        %LET _CLIENTPROJECTPATHHOST=;
588        %LET _CLIENTPROJECTNAME=;
589        %LET _SASPROGRAMFILE=;
2                                                          The SAS System                               09:26 Monday, March 18, 2024

590        %LET _SASPROGRAMFILEHOST=;
591        
592        ;*';*";*/;quit;run;
593        ODS _ALL_ CLOSE;
594        
595        
596        QUIT; RUN;
597        






yabwon
Onyx | Level 15

Great, but where is your log with the code I proposed ?

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

This comment seems to be not closed:

/* nicht möglich Name der Liste anzuzeigen!!!)

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

This line is badly commented out too:

/*%if NOT (&p_value. IN (&&&p_list.)) %then %do;                 /* */ 

 

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



obvisou
Fluorite | Level 6

Is here  something wrong? It was your suggestion. I don't need it. just deleted.

obvisou
Fluorite | Level 6

yes sorry, but this is not the mistake.

yabwon
Onyx | Level 15

Take the code I posted, copy it to your session, run it, and share the results, ok ?

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 32 replies
  • 2575 views
  • 1 like
  • 5 in conversation