Two macro variables to create a column name

Reply
Occasional Contributor
Posts: 8

Two macro variables to create a column name

Hi!

I want to use macro variables within if statements to check values and to create column names so I can make this code a generic and flexible as possible. What is wrong with this code?

per=4;

part=shk;

item=_ind;

data data_001;

          set data_001;


          if sas_scs= &per then

          do;


if &part_ind = 1 then  /* I mean: if column shk_ind = 1 then */

do;

int_&part_&per= 1; /* I mean: then column int_shk_4 = 1 */

slp_&part_&per=sas_qtr_pre ;

end;

else

do;

int_&part_&per = 0 ;

slp_&part_&per=0;

end;

end;

run;

Thanks!

Super User
Posts: 19,772

Re: Two macro variables to create a column name

When you use &part_ind SAS is looking for a macro variable called part_ind but I think you actually want shk_ind, and your macro variable is &part.

Use a period (.) to allow the macro variable to resolve:

&part._end

If you run your program with the following options the log will help you debug your issues.

Options SYMBOLGEN MPRINT;

New Contributor
Posts: 2

Re: Two macro variables to create a column name

Following is code that should do what you need.

%let per  = 4;
%let part = shk;
%let item = _ind;

    data data_001;
      set data_001;
      if sas_scs= &per
        then
          do;
          if &part._ind = 1 /* I mean: if column shk_ind = 1 then */
            then
              do;
              int_&part._&per = 1; /* I mean: then column int_shk_4 = 1 */
              slp_&part._&per = sas_qtr_pre ;
              end;
            else
              do;
              int_&part._&per = 0 ;
              slp_&part._&per = 0;
              end;
          end;
    run;

Ask a Question
Discussion stats
  • 2 replies
  • 182 views
  • 0 likes
  • 3 in conversation