<?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: Scoring survey data efficiently in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649396#M194680</link>
    <description>&lt;P&gt;You could use the fact that SAS considers TRUE = 1 and FALSE = 0 to write a single scoring expression:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;hcp_score = &lt;BR /&gt;&amp;nbsp;&amp;nbsp; (hcp_screen=1) * 1 &lt;BR /&gt;+ (hcp_screen=2) * 0.5&lt;BR /&gt;+ ...&lt;BR /&gt;+ (hcp_edu_change in (1,9)) * 0.25;&lt;/P&gt;</description>
    <pubDate>Wed, 20 May 2020 22:31:19 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2020-05-20T22:31:19Z</dc:date>
    <item>
      <title>Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649389#M194674</link>
      <description>&lt;P&gt;Hello! I'm writing code to analyze results from a multiple choice survey. My code is below. It works fine, but I'm pretty sure there is a better way of doing this than writing out hcp_score=hcp_score+1 a bunch of times. Does anyone have any suggestions? Some survey questions have multiple answer options with different point values, so I can't just sum the variables. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hcp_score=0;
	if hcp_screen=1 then hcp_score=hcp_score+1;
		else if hcp_screen=2 then hcp_score=hcp_score+0.5;
	if hcp_home=1 then hcp_score=hcp_score+1;
		else if hcp_home=2 then hcp_score=hcp_score+0.5;
	if hcp_list=1 then hcp_score=hcp_score+1;
	if hcp_cohort=1 then hcp_score=hcp_score+1;
	if hcp_restricted=1 then hcp_score=hcp_score+1;
	if hcp_singlefac=1 then hcp_score=hcp_score+1;
	if hcp_edu_covid=1 then hcp_score=hcp_score+0.25;
	if hcp_edu_sick=1 then hcp_score=hcp_score+0.25;
	if hcp_edu_ip=1 then hcp_score=hcp_score+0.25;
	if hcp_edu_change in (1,9) then hcp_score=hcp_score+0.25;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 May 2020 21:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649389#M194674</guid>
      <dc:creator>megsredl</dc:creator>
      <dc:date>2020-05-20T21:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649392#M194676</link>
      <description>It looks like those are weights applied to each variable if it's 1 or 1/9 in the case of screen/home/educ_change?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 20 May 2020 21:42:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649392#M194676</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-20T21:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649393#M194677</link>
      <description>&lt;P&gt;You could have an array of 10 variables, and a corresponding array of values to add (i.e. an array of 1's and 0.25, etc.), as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=v);
  set have;
  array check1 {*} hcp_screen hcp_home hcp_list hcp_cohort hcp_restricted 
                   hcp_singlefac hcp_edu_covid hcp_edu_sick hcp_edu_ip
                   hcp_edu_change ;
  array value1 {10} _temporary_ (6*1,4*0.25) ;

  hcp_score=0;
  do v=1 to dim(check1);
    if check1{v}=1 then hcp_score=hcp_score+value1{v} ;
  end;

  array check2 {*} hcp_screen hcp_home;
  do v=1 to dim(check2);
    if check2{v}=2 then hcp_score=hcp_score+0.5;
  end;

  if hcp_edu_change=9 then hcp_score=hcp_score+0.25;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above has 3 loops, one for each value to look for: 1, 2, and 9 (yes the check for 9 is not actually a loop, but think of it as a loop over on variable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you want to reduce it to a single loop, make a two-dimensional array, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=v);
  set have;
  array check {*} hcp_screen hcp_home hcp_list hcp_cohort hcp_restricted 
                   hcp_singlefac hcp_edu_covid hcp_edu_sick hcp_edu_ip hcp_edu_change ;

  array values {9,10} _temporary_
       (6*1, 4*0.25    /*row 1, results for variable=1 */
       ,2*0.5,8*.      /*row 2, results for variable=2 */
       ,60*.           /*rows 3-8*/
       ,9*.,0.25 )     /*row 9*/ ;

  hcp_score=0;
  do v=1 to 10;
    hcp_score=hcp_score+values{check{v},v};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The latter technique works only if the values to be searched for are integers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 21:58:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649393#M194677</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-05-20T21:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649394#M194678</link>
      <description>Hi! Sorry, the scoring wasn't really clear. Basically each question is worth one point. There are a few questions like hcp_screen and hcp_home that are always/sometimes/never questions, so "sometimes" is given 0.5 points. The last four questions are sub-questions within hcp_edu, which is worth one point total. The weird coding with hcp_edu_change is because there is an N/A option.</description>
      <pubDate>Wed, 20 May 2020 21:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649394#M194678</guid>
      <dc:creator>megsredl</dc:creator>
      <dc:date>2020-05-20T21:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649395#M194679</link>
      <description>&lt;P&gt;Thank you, this is very helpful!&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 22:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649395#M194679</guid>
      <dc:creator>megsredl</dc:creator>
      <dc:date>2020-05-20T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649396#M194680</link>
      <description>&lt;P&gt;You could use the fact that SAS considers TRUE = 1 and FALSE = 0 to write a single scoring expression:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;hcp_score = &lt;BR /&gt;&amp;nbsp;&amp;nbsp; (hcp_screen=1) * 1 &lt;BR /&gt;+ (hcp_screen=2) * 0.5&lt;BR /&gt;+ ...&lt;BR /&gt;+ (hcp_edu_change in (1,9)) * 0.25;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 22:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649396#M194680</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-05-20T22:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Scoring survey data efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649398#M194681</link>
      <description>&lt;P&gt;"Efficiency" may depend on how many variables with similar values.&lt;/P&gt;
&lt;P&gt;Similar calculations for multiple variables often indicates an array approach might work. So the block of the comparisons to single values could be done a couple of ways. &lt;/P&gt;
&lt;P&gt;Here is one:&lt;/P&gt;
&lt;PRE&gt;data trial;
   set have;
   hcp_score=0;
   if hcp_screen=1 then hcp_score=hcp_score+1;
   else if hcp_screen=2 then hcp_score=hcp_score+0.5;
   if hcp_home=1 then hcp_score=hcp_score+1;
   else if hcp_home=2 then hcp_score=hcp_score+0.5;
	if hcp_edu_change in (1,9) then hcp_score=hcp_score+0.25;
   /* below are the SINGLE value comparisons*/
   array vars hcp_list hcp_cohort hcp_restricted hcp_singlefac 
              hcp_edu_covid hcp_edu_sick hcp_edu_ip ;
   /* this array holds the COMPARISON values for the variables
      IN ORDER*/
   array vals {7} _temporary_ (1,1,1,1,1,1,1);
   /* this has the score additions*/
   array sc   {7} _temporary_ (1,1,1,1, 0.25,0.25,0.25);
   do i=1 to dim(vars);
      if vars[i]=vals[i] then hcp_score=hcp_score + sc[i];
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;The only change shown is for the Single value comparisons. The first array has the variables you need to compare, the second array, vals, contains the values that the variables are tested for equality and third has the amount to add to the score. The order of the variables, values and score additions must match in order.&lt;/P&gt;
&lt;P&gt;You might see right off had that if I have to add 10 more variables I add them to the VARS list, then the value to compare, then score. The do loop with the number of elements in the vars list takes care of all of the conditional additions to the score total.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this is really simple for single values. You could use it for multiple values by placing the variable on the list twice with the corresponding Values for comparsion and the corresponding score additions. That just is a tad harder to see right away.&lt;/P&gt;
&lt;P&gt;If the above test code I show above works as expected then you could try&lt;/P&gt;
&lt;PRE&gt;data trial;
   set have;
   hcp_score=0;
   array vars hcp_list hcp_cohort hcp_restricted hcp_singlefac 
              hcp_edu_covid hcp_edu_sick hcp_edu_ip 
              hcp_screen hcp_screen
              hcp_home hcp_home
              hcp_edu_change hcp_edu_change
  ;
   /* this array holds the COMPARISON values for the variables
      IN ORDER*/
   array vals {13} _temporary_ (1,1,1,1,1,1,1,1,2,1,2,1,9);
   /* this has the score additions*/
   array sc   {13} _temporary_ (1,1,1,1, 0.25,0.25,0.25,1,0.5,1,0.5, 0.25,0.25);
   do i=1 to dim(vars);
      if vars[i]=vals[i] then hcp_score=hcp_score + sc[i];
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;Note that I just added the multi-value comparison variables to end of the list with the corresponding comparison and score values and adjusted the size of the temporary arrays to match.&lt;/P&gt;
&lt;P&gt;One of the drawbacks of this approach is having a mismatched number of values and variables will likely cause&amp;nbsp; the error: Array Subscript out of range&lt;/P&gt;
&lt;P&gt;And one or more warnings about "partial array initialization" (not enough values) or "Too many values for initialization of the array".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 22:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scoring-survey-data-efficiently/m-p/649398#M194681</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-20T22:53:25Z</dc:date>
    </item>
  </channel>
</rss>

