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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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