<?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 Re: Adding variables within ID to create new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735819#M229224</link>
    <description>&lt;P&gt;This statement is a problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a = 2 | 3 then score1 = 2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;True, | means "or".&amp;nbsp; But it is unrelated to the value of A.&amp;nbsp; It's equivalent to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (3) or (a=2) then score1=2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the condition 3 is always true.&amp;nbsp; A simple replacement will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a in (2, 3) then score1 = 2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Apr 2021 02:24:01 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-04-21T02:24:01Z</dc:date>
    <item>
      <title>Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735817#M229223</link>
      <description>&lt;PRE&gt;data scale; set cleaned; by ID;
	IF a = 1 then score1 = 1;
	if a = 2 | 3 then score1 = 2;
	if a &amp;gt;= 4 &amp;amp; a &amp;lt;= 6 then score1 = 3;
	IF b = 1 then score2 = 1;
	if b = 2 | 3 then score2 = 2;
	if b &amp;gt;= 4 &amp;amp; b &amp;lt;= 6 then score2 = 3;
	IF c = 1 then score3 = 1;
	if c = 2 | 3 then score3 = 2;
	if c &amp;gt;= 4 &amp;amp; c &amp;lt;= 6 then score3 = 3;
	total_score = score + score + score;

	if d = 1 then act1 = 2; else act1 = 1;
	if e = 1 then act2 = 2; else act2 = 1;
	if f = 1 then act3 = 2; else act3 = 1;
	if g = 1 then act4 = 2; else act4 = 1;
	total_act = act1 + act2 + act3 + act4;
run;&lt;/PRE&gt;&lt;P&gt;This is the code that I have been trying. I know for sure there are IDs in which a, b, and c are all 1 but there are no cases where total_score = 3. I would like to know if there is a way to fix my code so that I can get my desired output.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 00:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735817#M229223</guid>
      <dc:creator>ercksh8</dc:creator>
      <dc:date>2021-04-21T00:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735819#M229224</link>
      <description>&lt;P&gt;This statement is a problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a = 2 | 3 then score1 = 2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;True, | means "or".&amp;nbsp; But it is unrelated to the value of A.&amp;nbsp; It's equivalent to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (3) or (a=2) then score1=2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the condition 3 is always true.&amp;nbsp; A simple replacement will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a in (2, 3) then score1 = 2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 02:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735819#M229224</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-04-21T02:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735820#M229225</link>
      <description>&lt;P&gt;There are two obvious mistakes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First is you probably want to add ELSE statements in there are the appropriate places.&lt;/P&gt;
&lt;P&gt;Second you have places where you are treating integer constants as boolean expressions.&amp;nbsp; &amp;nbsp;So this expression is always true since whether or not A is equal to 2 does not matter as 3 is always treated is TRUE.&amp;nbsp;SAS will treat any number other than zero (or a missing value) as TRUE.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a = 2 | 3 then score1 = 2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also add some parentheses into your expressions to make sure you know the order they will be evaluated. It will also make it easier to understand if you used the OR and AND keywords instead of trying to save a couple of characters of typing by using (potentially confusing) | and &amp;amp; symbols instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (a = 1) then score1 = 1;
else if (a = 2) or (a = 3) then score1 = 2;
else if (a &amp;gt;= 4) and (a &amp;lt;= 6) then score1 = 3;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 02:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735820#M229225</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-21T02:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735826#M229230</link>
      <description>&lt;P&gt;Seems to be a great job for a format and some arrays:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length a b c d e f g 8;
   
   array scores[3] a b c;
   array acts[4] d--g;
   
   do _n_ = 1 to 10;
      do _i = 1 to dim(scores);  
         scores[_i] = rand('Integer', 1, 6);
      end;
      
      do _i = 1 to dim(acts);
         acts[_i] = rand('Integer', 0, 1);
      end;
      
      output;
   end;

   drop _i;
run;

proc format;
   invalue ScoreFmt
      1 = 1
      2, 3 = 2
      4 - 6 = 3
   ;
run;


data want;
   set have;
      
   array toScores[3] a b c;
   array scores[3];
   
   array toActs[4] d--g;
   array acts[4];
   
   do _i = 1 to dim(toScores);
      scores[_i] = input(put(toScores[_i], 1.), ScoreFmt.);
   end;
   
   total_scores = sum(of scores[*]);
   
   do _i = 1 to dim(toActs);
      acts[_i] = (toActs[_i] = 1) + 1;
   end;
   
   total_acts = sum(of acts[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 05:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735826#M229230</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-04-21T05:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735828#M229231</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total_score = score + score + score;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Unless score is present in the incoming dataset, this statement will create missing values throughout. I guess you wanted&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total_score = score1 + score2 + score3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or, even better&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total_score = sum(score1,score2,score3);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 06:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735828#M229231</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-21T06:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Adding variables within ID to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735883#M229246</link>
      <description>&lt;P&gt;Similar approach as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp; but without arrays to not "overload" you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  invalue score
    1 =1
    2-3=2
    4-6=3
    other=.
  ;
run;

data scale;
  set cleaned;
  by ID;
  score1=input(a,score.);
  score2=input(b,score.);
  score3=input(c,score.);
  total_score=sum(score1,score2,score3);

  if d = 1 then act1 = 2;  else act1 = 1;
  if e = 1 then act2 = 2;  else act2 = 1;
  if f = 1 then act3 = 2;  else act3 = 1;
  if g = 1 then act4 = 2;  else act4 = 1;
  total_act = sum(act1,act2,act3,act4);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 09:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-variables-within-ID-to-create-new-variable/m-p/735883#M229246</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-21T09:06:42Z</dc:date>
    </item>
  </channel>
</rss>

