BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aquamarin
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
SuryaKiran
Meteorite | Level 14

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
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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 ;
Aquamarin
Calcite | Level 5

Thats realy astounding 😉

 

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.

ballardw
Super User

@Aquamarin wrote:

Thats realy astounding 😉

 

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.

Tom
Super User Tom
Super User

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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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