DATA Step, Macro, Functions and more

Concatenating SAS macros variables and resolving it as another macro

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Concatenating SAS macros variables and resolving it as another macro

Hi all

 

In the below code, I am trying to resolve 'need' macro using status macro. I have a particular scenario for which I need to handle things this way.

 

%let need = 1;
%let test1 = need;
%let status = %str(&)&test1;
%let status1 = %sysfunc(cats(&,&test1));

%put &status;
%put &status1;

 

The %put statement with &status resolves &need where as &status1 resolves 1. I need to resolve 1.

 

I want to avoid using cats because it is priting blank lines in my log for some strange unknown reasons just with one particular %let statement. I have cats in other places too, and they also don't seem to create any issue. I am unable to recreate this blank space issue outside my application and hence I was trying to get rid of cats and achieve the same concatenation with something else. Hence I tried using %str, but it is not making things resolve as a macro. 

 

Any suggestion to handle this case.

 

thanks!

 

 


Accepted Solutions
Solution
‎12-15-2016 03:53 PM
Super User
Posts: 5,099

Re: Concatenating SAS macros variables and resolving it as another macro

To get it to resolve, apply %UNQUOTE:

 

%put %unquote(&status);

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,430

Re: Concatenating SAS macros variables and resolving it as another macro

Far simpler to use Base SAS (it is the programming language after all).  To be honest, I was going to provide an example, but I really can't even figure out what its meant to do?  I.e. the end product is the number 1, but so i jut referencing &need, so all of that code is not necessary.

 

Contributor
Posts: 20

Re: Concatenating SAS macros variables and resolving it as another macro

Okay, let me give you bit more background. So I have this macro..

 

This is the flags table:

 

sa_code process_flag

need         P

sale           N

 

data _null_;

set flags;

call symput(sa_code, process_flag );

run;

 

So the call symput throws 2 macro (need and sale) variables... 'need' with value 'P' and 'sale' with value 'C'.

 

%macro run_sa(sa_code);

 

%let sa_status = %sysfunc(cats(&,&sa_code));

 

%if &sa_status =  'P' %then %do;

%put "Test";

%end;

 

%mend run_sa;

 

data _null_;
set flags2;
file code;
put '%run_subj_area ( subja_name = %sysfunc(strip( ' sa_code ' )));';
where process_flag NE 'C';
run;

%inc code/source2;

 

In the run_sa macro, if "sa_code" is directly used instead of sa_status,it would not resolve what is stored in macro need,instead it would resolve just need. Hope you got it!

Super User
Posts: 10,550

Re: Concatenating SAS macros variables and resolving it as another macro

No need to include %sysfunc. Assuming you have run the data _null_ as shown:

%put &&need;
%put &&sale;

%let sa_code=need;
%let sa_status = &&&sa_code;
%put &sa_status;
Super User
Posts: 5,099

Re: Concatenating SAS macros variables and resolving it as another macro

The first point to clear up (perhaps the only point?) is this.  This comparison is always false:

 

%if &sa_status = 'P' %then %do;

 

This asks macro language to look for three characters within &sa_status:  a quote, then P, then another quote.  And &sa_status never has a quote as part of its value.  This would be the correct way to have macro language examine whether the value is P:

 

%if &sa_status = P %then %do;

 

Maybe this would clear up everything, maybe not.  But it's the right first step.

 

Also note, you would be better off switching from SYMPUT to SYMPUTX.  If you have any trailing blanks in the second parameter, SYMPUT leaves them as part of the assigned value, while SYMPUTX automatically removes them.

Contributor
Posts: 20

Re: Concatenating SAS macros variables and resolving it as another macro

Sorry, I have that without quotes in my actual code. I added quotes by mistake while typing out the example. Thanks for pointing out any ways.

 

But even getting rid of cats and using %str + %unquote approach mentioned here, I still have 5 or 6 blank pages getting printed in log for no reason. I am not sure why. It is getting printed exactly after the %let statement. I was assuming that default variable length that results out of cats statement to be the reason for the blank lines. But after trying out other approaches to concatenate looks like it is not the case. I am unable to replicate the blank space issue outside my application despite keeping everything similar to the scenario there.

 

I understand it is a off topic to this question in discussion, but just wanted to put it down as I provided similar example. I have similar usage with %let in other places, but none of them have any issues.

Super User
Posts: 10,550

Re: Concatenating SAS macros variables and resolving it as another macro

What ODS destination are you sending output to? Since HTML generally doesn't have a concept of page then you must be sending the data somewhere that uses pages and each one can have different ways to create an unintended page break.

 

 

Are you using the Document procedure?

Are you using a report procedure such as Tabulate or Report that supports a page dimension in output?

Are you using the NEWLINE function anywhere? Large values for an argument would create pages, or NBSPACE with very large values could.

 

Are you using a custom style? You may have introduced something places page breaks in unexpected places.

Do you have text data that possibly comes from a different operation system that you are printing or including in a text display? UNIX systems displaying Windows or vice versa treat Carriage Return and Linefeed values differently (and Mac is yet another issue)?

Does you data originate in Excel where someone may have used Alt-Enter to place line breaks within a single value?

 

 

Contributor
Posts: 20

Re: Concatenating SAS macros variables and resolving it as another macro


Hi Ballard

Thanks for your inputs. I just figured out what was going wrong. I was generating a handful of macros from a dataset using call symput, and it seems those macros were coming up with additional spaces which was causing logs to have blank lines. I changed the call symput to call symputx and that fixed the blank space issue in the log.

 

In the below example, 

 

sa_code process_flag

need         P

sale           N

 

data _null_;

set flags;

call symput(sa_code, process_flag ); --changed this to---->  call symputx(sa_code, process_flag );

run;

 

So the call symput throws 2 macro (need and sale) variables... 'need' with value 'P' and 'sale' with value 'C'.

 

%macro run_sa(sa_code);

 

%let sa_status = %sysfunc(cats(&,&sa_code));

 

%if &sa_status =  'P' %then %do;

%put "Test";

%end;

 

%mend run_sa;

 

data _null_;
set flags2;
file code;
put '%run_subj_area ( subja_name = %sysfunc(strip( ' sa_code ' )));';
where process_flag NE 'C';
run;

%inc code/source2;

Regular Contributor
Posts: 194

Re: Concatenating SAS macros variables and resolving it as another macro

I'm not sure i understand your problem.

Does this answer your question ?

%let need = 1;
%let test1 = need;
%let status = &&&test1;

%put &status;
Solution
‎12-15-2016 03:53 PM
Super User
Posts: 5,099

Re: Concatenating SAS macros variables and resolving it as another macro

To get it to resolve, apply %UNQUOTE:

 

%put %unquote(&status);

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 311 views
  • 3 likes
  • 5 in conversation