Desktop productivity for business analysts and programmers

Prompt macro values equal 0 after catx

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Prompt macro values equal 0 after catx

Hi,

i have a prompt where the user can select, for example:

 

Car

Plane

Title

 

The prompt is named "test"... and if i select all of them, than test1=Car, test2=Plane, test3=Title gets resolved right in the log.

 

Now i need to add them into one variable, so:

 

%let one = &test;
%let two = test;

%macro xyz;
%if &test_count GT 0 %then %do i=2 %to &test_count;
%let one = %sysfunc(catx(%quote(,), &one, &&&two.&i));
%end;
....

 

Result if i select for example "Plane"

Plane

 

Result if i select 2

0,0

 

Result 3

0,0,0

 

Does anybody see where the mistake is?


Accepted Solutions
Solution
a week ago
Super User
Posts: 6,624

Re: Prompt macro values equal 0 after catx

Posted in reply to Aquamarin

I would start by cutting the problem in half.  Is it a problem with the values of the macro variables, or is it a problem with the concatenation?  Narrow down the scope of the problem by adding:

 

%put _user_;

 

SAS will clarify what is in all of your macro variables.

 

Also, consider a simpler form of concatenation:

 

%let one = &one,&&&two.&i;

 

I'm not sure if it would keep extra blanks in the result.  But if it gives the right answer without extra blanks, it's certainly simpler.

View solution in original post


All Replies
Valued Guide
Posts: 558

Re: Prompt macro values equal 0 after catx

Posted in reply to Aquamarin

What are you trying to achieve?

 

You may need to change the do loop like:

 

%let one = &test;
%let two = test;

%macro xyz;
%if &test_count GT 0 %then %do;
%do  i=2 %to &test_count;
%let one = %sysfunc(catx(%quote(,), &one, &&&two.&i));
%end;
%end;
Thanks,
Suryakiran
Solution
a week ago
Super User
Posts: 6,624

Re: Prompt macro values equal 0 after catx

Posted in reply to Aquamarin

I would start by cutting the problem in half.  Is it a problem with the values of the macro variables, or is it a problem with the concatenation?  Narrow down the scope of the problem by adding:

 

%put _user_;

 

SAS will clarify what is in all of your macro variables.

 

Also, consider a simpler form of concatenation:

 

%let one = &one,&&&two.&i;

 

I'm not sure if it would keep extra blanks in the result.  But if it gives the right answer without extra blanks, it's certainly simpler.

Super User
Super User
Posts: 7,931

Re: Prompt macro values equal 0 after catx

Posted in reply to Aquamarin

Don't use CATX() functions in macro code. That is a data step function that makes no sense to use in macro code.  You could use macro code to generate CATX() function syntax that will run in a data step.  But in a macro to combine macro variable values you just need to expand the macro variable where you want it to appear.

%let one=&one.,&&&two.&i ;
New Contributor
Posts: 2

Re: Prompt macro values equal 0 after catx

Thats realy astounding Smiley Wink

 

Thank you both very much, it works now.

 

One question, why dont work with catx in macros? Did that all the time and it worked, till now.

But your code is much more simple.

Super User
Posts: 13,292

Re: Prompt macro values equal 0 after catx

Posted in reply to Aquamarin

@Aquamarin wrote:

Thats realy astounding Smiley Wink

 

Thank you both very much, it works now.

 

One question, why dont work with catx in macros? Did that all the time and it worked, till now.

But your code is much more simple.


Without knowing the values of all of the macro variables it is going to be a guess. You are referencing several variables that are not shown values: &testcount &test2 to &testX (where X=&test_count).

 

And you really should show what you would expect to get.

Super User
Super User
Posts: 7,931

Re: Prompt macro values equal 0 after catx

[ Edited ]
Posted in reply to Aquamarin

The whole purpose of the CAT... series of data step functions is to make it easier to convert variables into text and concatenate the text. 

 

In macro code everything is text and you are always concatenating text. That is the basic operation of a macro processor!

So no need to complicate your program by adding in function calls that don't add any extra functionality.

 

In data step character variables are padded with spaces to fill the length of the variable.  In a data step the automatic conversion from a number to a character string normally results in leading spaces. But in macro code you do not have spaces padded onto the ends of your macro variables, unless you worked extra hard to add them.  So the trimming features of the CAT..() functions is not needed in macro code.

 

Also when you call a SAS function that can accept both numeric and character arguments the macro processor needs to figure out what type of value to pass to the function.  It can get confused trying to do that and generate errors.

 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 141 views
  • 3 likes
  • 5 in conversation