data have;
infile datalines delimiter='~';
length Group $7. Group_Num 3. Interim_Value $1. Value_1 $50.;
input Group $ Group_Num Interim_Value $ Final_Value $ ;
datalines;
Group_1~1~1~1
Group_1~1~1~1
Group_1~1~0~0
;
data want;
set have;
if Interim_Value=1 then
compress("Value_"||Group_Num)=0; *This is getting read as a string/array - I want it to read in as a dynamic variable name;
run;
data want;
infile datalines delimiter='~';
length Group $7. Group_Num 3. Interim_Value $1. Final_Value $50.;
input Group $ Group_Num Interim_Value $ Final_Value $ ;
datalines;
Group_1~1~1~0
Group_1~1~1~0
Group_1~1~0~0
;
I'm trying to dynamically re-assign the value of an unknown variable. However, I run into trouble because SAS is reading the unknown variable as a string instead of as a variable. I am aware that there are other ways to solve this problem the way that I presented it - I am interested in this one specifically. Is this something that SAS can handle? Right now, SAS reads my dynamic variable as a string instead of a variable, and returns an error.
Thanks for the help!
You cannot change the CODE of a data step once the step has started running. You cannot change the set of variables in a data step after the step has started running. So trying to use the value of a variable as the name of the target variable in an assignment statement is never going to work.
The normal way to deal with this is via the ARRAY concept. An ARRAY is a method to allow access a variable by an index into the list of variables that the array contains.
Before you start running the data step you need to know how many variables you are going create.
data have;
infile datalines delimiter='~';
length Group $7 Group_Num 8 Value 8 ;
input Group Group_Num Value ;
datalines;
Group_1~1~1
Group_1~2~1
Group_1~3~0
;
data want;
set have;
array value_ [5] ;
value_ [ group_num ] = value;
run;
proc print;
run;
Results:
Group_ Obs Group Num Value value_1 value_2 value_3 value_4 value_5 1 Group_1 1 1 1 . . . . 2 Group_1 2 1 . 1 . . . 3 Group_1 3 0 . . 0 . .
Notes on your data steps:
You cannot change the CODE of a data step once the step has started running. You cannot change the set of variables in a data step after the step has started running. So trying to use the value of a variable as the name of the target variable in an assignment statement is never going to work.
The normal way to deal with this is via the ARRAY concept. An ARRAY is a method to allow access a variable by an index into the list of variables that the array contains.
Before you start running the data step you need to know how many variables you are going create.
data have;
infile datalines delimiter='~';
length Group $7 Group_Num 8 Value 8 ;
input Group Group_Num Value ;
datalines;
Group_1~1~1
Group_1~2~1
Group_1~3~0
;
data want;
set have;
array value_ [5] ;
value_ [ group_num ] = value;
run;
proc print;
run;
Results:
Group_ Obs Group Num Value value_1 value_2 value_3 value_4 value_5 1 Group_1 1 1 1 . . . . 2 Group_1 2 1 . 1 . . . 3 Group_1 3 0 . . 0 . .
Notes on your data steps:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.