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

I did use your Code. If not, please share the hole code. Copy my last and correct it. If you want to help, please Thank you.

Don't want to discuss about programmingstyle, thanks.

 

 

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 .

obvisou
Fluorite | Level 6

12:32 PM you posted this, with extra Semicolon on the end (%then &current;)

%macro check_and_add(current,new);
%if %sysfunc(findw(&current,&new,,s)) %then &current;
%else &current &new;
%mend;

After your last post you mentioned without Semicolon and now it works! Thank you very much. I have to accept that the same Macro-Variable must be entered twice:

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

And about your comment why use %put-Statments. Please do not forget, we are using SAS, which is sometimes a pain. It makes a big difference if you write Code in a Macro, in Data Set, outside Macro etc... I prefer to write more Code, who is working almost everywhere.

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2176 views
  • 1 like
  • 5 in conversation