DATA Step, Macro, Functions and more

counting variables

Trusted Advisor
Posts: 1,204

counting variables

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.

Super User
Posts: 5,085

Re: counting variables

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;


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.

Trusted Advisor
Posts: 1,204

Re: counting variables

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;


Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation