<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Dynamic Variable on Left Side of Variable Assignment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Variable-on-Left-Side-of-Variable-Assignment/m-p/837886#M331318</link>
    <description>&lt;PRE&gt;&lt;CODE class=""&gt;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
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help!&lt;/P&gt;</description>
    <pubDate>Tue, 11 Oct 2022 16:45:26 GMT</pubDate>
    <dc:creator>theponcer</dc:creator>
    <dc:date>2022-10-11T16:45:26Z</dc:date>
    <item>
      <title>Dynamic Variable on Left Side of Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Variable-on-Left-Side-of-Variable-Assignment/m-p/837886#M331318</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;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
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 16:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Variable-on-Left-Side-of-Variable-Assignment/m-p/837886#M331318</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2022-10-11T16:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Variable on Left Side of Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Variable-on-Left-Side-of-Variable-Assignment/m-p/837894#M331320</link>
      <description>&lt;P&gt;You cannot change the CODE of a data step once the step has started running.&amp;nbsp; You cannot change the set of variables in a data step after the step has started running.&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The normal way to deal with this is via the ARRAY concept.&amp;nbsp; An ARRAY is a method to allow access a variable by an index into the list of variables that the array contains.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before you start running the data step you need to know how many variables you are going create.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                  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          .          .
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes on your data steps:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Do not include periods when setting the LENGTH of a variable.&amp;nbsp; The length can only be an integer value so no decimal place is needed.&lt;/LI&gt;
&lt;LI&gt;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.&lt;/LI&gt;
&lt;LI&gt;If you have already defined the variable as character there is no need to include the $ token in the INPUT statement.&lt;/LI&gt;
&lt;LI&gt;Do NOT intend the DATALINES statement.&amp;nbsp; This will help you remember not to indent the actual lines of data.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 17:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Variable-on-Left-Side-of-Variable-Assignment/m-p/837894#M331320</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-11T17:34:34Z</dc:date>
    </item>
  </channel>
</rss>

