0
votes

In a SAS Data Step i have a character variable called "varName". This variable stores the name of another variable. In below's example, it stores the name of the numeric variable "changeMe":

data TMP;
   length
      varName $32
      changeMe 8
      ;
   varName = ‘changeMe’;
   /*??? How to change the content of variable that varName holds ???*/
run;

Now the question is: how do i change the content of the variable that varName holds?

The use case would be that varName acts as a dynamic pointer to different variables that i want to manipulate in a big SAS Data Set.

2
Looks like you have a transaction file. Check out the UPDATE statement. Also do you have the data in text file? Check out the named input style. documentation.sas.com/… We used to use that for creating transaction datasets.Tom

2 Answers

0
votes

DATA Step does not directly provide for named indirect assignment.

In some cases, the indirect assignment requirement might indicate you want to perform a Proc TRANSPOSE data transformation. If the variable names and values are provided in a transaction data set, and the data has BY group variables, your better solution might be to TRANSPOSE the transaction data and merge that transform to the master data using an UPDATE or MODIFY statement.

Regardless, you can array variables of a given type and iterate the array looking for the target requiring assignment.

Example:

data want;
  set sashelp.class;

  varname = 'name';
  varvalue = 'Scooter';

  array chars _character_;

  do _n_ = 1 to dim(chars);
    if upcase (vname(chars(_n_))) = upcase(varname) then do;
      chars(_n_) = varvalue;
    end;
  end;
run;

Output
enter image description here

0
votes

call execute() is a highly feasible solution.

data TMP;
   length
      varName $32
      changeMe 8
      ;
   varName = 'changeMe';
run;

data _null_;
  set TMP end=eof;

  if _n_ = 1 then call execute('data %trim(&syslast.); modify %trim(&syslast.);');
  call execute(cats(varName)||' = rand("uniform",1,0);');
  if eof then call execute('run;');
run;

Log:

NOTE: CALL EXECUTE generated line.
1   + data WORK.TMP; modify WORK.TMP;
2   + changeMe = rand("uniform",1,0);
3   + run;

NOTE: There were 1 observations read from the data set WORK.TMP.
NOTE: The data set WORK.TMP has been updated.  There were 1 observations rewritten, 0 observations
      added and 0 observations deleted.