BookmarkSubscribeRSS Feed
stat_sas
Ammonite | Level 13

Hi Experts,

Below is an extract from a macro program.

%if &_N_Var_values_>1 and &_N_Var_values_<=10 %then

%let var_count=catx(' ',&var_name);

I am trying to concatenate variable names having number of levels from 1 to 10 in  a macro variable var_count but not getting the desired results. Please advise how can I modify

%let var_count=catx(' ',&var_name) so that I can have variables in the following format

a b d g (Qualifying constraint from 1 to 10).

Thanks in advance for your suggestions.

2 REPLIES 2
Astounding
PROC Star

This approach can be done, but involves several complications.  To use CATX in macro language, you need to place it inside %SYSFUNC.  To refer to a blank in macro language, you have to refer to %str( ) rather than ' '.  And what does &VAR_NAME actually refer to?  Don't you have 10 macro variables rather than a single &VAR_NAME?  Otherwise what are you concatenating?  Or does &VAR_NAME already contain a series of commas embedded within it?

On the off-chance that the answer is that you have a set of macro variables &VAR_NAME1, &VAR_NAME2, etc., here is an easier way to concatenate them:

%local i var_count;

%do i=1 %to &_N_Var_values_;

  %let var_count = &var_count &&var_name&i;

%end;

I'm not sure I have the right picture here.  Why would you want to use the name &VAR_COUNT to hold the final string of concatenated variables?

Maybe you can clarify what you are trying to accomplish.

Good luck.

stat_sas
Ammonite | Level 13

Thanks Astounding - I've a lot of variables in dataset and I am trying to create a macro variable by selecting  variables containing maximum 10 different values for classification analyses. Code mentioned above is helpful in identifying required variables. If I need to access var_count variable outside the macro what changes should I make in the syntax? Secondly on first iteration what value is being assigned to  &var_count?

%let var_count = &var_count &&var_name&i;

Regards

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
  • 2 replies
  • 955 views
  • 0 likes
  • 2 in conversation