BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
theponcer
Quartz | Level 8
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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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:

  • Do not include periods when setting the LENGTH of a variable.  The length can only be an integer value so no decimal place is needed.
  • Do not set numeric variables to a length that is less than the full 8 bytes required to store the 64-bit floating point numbers that SAS uses.
  • If you have already defined the variable as character there is no need to include the $ token in the INPUT statement.
  • Do NOT intend the DATALINES statement.  This will help you remember not to indent the actual lines of data.

 

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

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:

  • Do not include periods when setting the LENGTH of a variable.  The length can only be an integer value so no decimal place is needed.
  • Do not set numeric variables to a length that is less than the full 8 bytes required to store the 64-bit floating point numbers that SAS uses.
  • If you have already defined the variable as character there is no need to include the $ token in the INPUT statement.
  • Do NOT intend the DATALINES statement.  This will help you remember not to indent the actual lines of data.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 239 views
  • 1 like
  • 2 in conversation