DATA Step, Macro, Functions and more

assign values to variables which started out as values

Reply
Contributor
Posts: 32

assign values to variables which started out as values


Hello,

 

How do I assign values to the values within macro variables which I want to be variables in turn?

 

For example, let's say I assign a number of question codes:

 

data question_vars;
 input question_code $25.;
 datalines;
QUESTION_CODE001

QUESTION_CODE002

QUESTION_CODE003
run;

 

proc sql noprint;
    select question_code into: questioncodelist separated by ' '
    from question_vars;
quit;

 

So, from here, how do I assign a value to QUESTION_CODE001?  In other words, I want to make the values within question_code  be variables unto which I can assign values to.

 

Kind regards,

Mark

PROC Star
Posts: 2,369

Re: assign values to variables which started out as values

Like this?

data question_vars;
 input question_code $25.;
 datalines;
QUESTION_CODE001
QUESTION_CODE002
QUESTION_CODE003
run;

proc sql noprint;
  select catt('&',question_code) into: questioncodelist separated by ' '
  from question_vars;
quit;

%let question_code001=a;
%let question_code002=b;
%let question_code003=c;

%put &=questioncodelist;

QUESTIONCODELIST=a b c

Contributor
Posts: 32

Re: assign values to variables which started out as values

Thank you very much for your reply!

 

I'm finding this one hard to put into words to be honest.

 

Basically I would like to be able to read through a dataset and I would like to make the value of a variable, in turn be another variable which I can assign another value to.

 

For example let's say an observation contains:

Columns:             var1  var2  var3

Row 1 contents:  AAA  BBB  CCC

 

So as I am reading through var1, I would like to be able to make its value i.e. AAA in turn be a variable name to which I can assign var3.

 

In other words:

let the value of (var1) = the value of var3

 

so that new variable AAA = CCC.

 

Many thanks!

 

 

 

Super User
Posts: 23,754

Re: assign values to variables which started out as values

CALL SYMPUTX(). 

 

 

PROC Star
Posts: 2,369

Re: assign values to variables which started out as values

Maybe this then?

data _null_;
  retain VAR1 'AAA'  VAR3 'CCC';
  call symputx(VAR1, VAR3);
  call execute('%put &='||VAR1);
run;

AAA=CCC

 

Ask a Question
Discussion stats
  • 4 replies
  • 92 views
  • 4 likes
  • 3 in conversation