DATA Step, Macro, Functions and more

How to scan a macro variable and use the result as a variable name

Accepted Solution Solved
Reply
PROC Star
Posts: 7,468
Accepted Solution

How to scan a macro variable and use the result as a variable name

The following, except for the fourth line from the bottom of the code, does what I want.  However, in the section:

  do _i=1 to 4;

    want((_n_-1)*4+_i)=scan("&vars.",_i);

  end;

I'd like to assign the values of var1, var2, var3 and var4

What do I have to change/add/modify in order to obtain that result?

TIA,

Art

data have (drop=months);

  format id best32.;

  input date (ind1-ind4) ($) ;

  informat date date9.;

  format date yymon7.;

  array other_variable(2);

  do id=1 to 2;

    other_variable(id)=id;

  end;

  other_variable1=1;

  other_variable2=2;

  do id=1 to 10;

    date="01dec2010"d;

    do months=1 to 12;

      date=intnx('month',date,1);

      if not(months eq 9 and mod(id,2)) then output;

    end;

  end;

  cards;

01dec2010 A B C D

;

%let filein=have;

%let by_variable=id;

%let vars=ind1 ind2 ind3 ind4;

%let numvars=4;

%let id_label=date;

proc sql noprint;

  select distinct 'ind1_'||put(date,yymon7.)||

                  ' ind2_'||put(date,yymon7.)||

                  ' ind3_'||put(date,yymon7.)||

                  ' ind4_'||put(date,yymon7.),

                  date

    into :varlist separated by " ",

         :junk

      from &filein. (obs=1000) /*must be big enough to capture all months*/

        order by &id_label.

  ;

quit;

data want;

  set &filein.;

  by &by_variable.;

  array want(*) $ &varlist.;

  retain want;

  if first.id then call missing(of want(*));

  _n_=month(date);

  do _i=1 to 4;

    want((_n_-1)*4+_i)=scan("&vars.",_i);

  end;

  drop &id_label. _: &vars.;

  if last.&by_variable. then output;

run;


Accepted Solutions
Solution
‎01-18-2013 12:59 AM
Super User
Super User
Posts: 7,039

Re: How to scan a macro variable and use the result as a variable name

Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.

want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));


View solution in original post


All Replies
Super User
Super User
Posts: 7,039

Re: How to scan a macro variable and use the result as a variable name

You need another ARRAY.

array want(*) $ &varlist.;

array have $ &vars ;

   ...

want((_n_-1)*4+_i)=have(_i) ;

PROC Star
Posts: 7,468

Re: How to scan a macro variable and use the result as a variable name

Tom,  I'll come back and mark your response as "correct" rather than just "helpful" if no one can come up with a way of doing what I'd really like to do.  However, using an array definitely solved the problem.

Solution
‎01-18-2013 12:59 AM
Super User
Super User
Posts: 7,039

Re: How to scan a macro variable and use the result as a variable name

Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.

want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));


PROC Star
Posts: 7,468

Re: How to scan a macro variable and use the result as a variable name

: That was definitely what I was looking for and, yes, it would allow combining character and numeric variables if I changed the program so that it wasn't forcing all of the results into a single array.

However, I'm not going to use it, after all, as it came with a high price.  It doubled the cpu time required for the overall process.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 248 views
  • 3 likes
  • 2 in conversation