<?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: How to subtract a row from all other rows in data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939398#M368842</link>
    <description>&lt;P&gt;That is brilliant,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;. I'm starting to think that SAS is very powerful!&lt;/P&gt;</description>
    <pubDate>Thu, 15 Aug 2024 11:42:16 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-08-15T11:42:16Z</dc:date>
    <item>
      <title>How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939365#M368826</link>
      <description>&lt;P&gt;This is my code so far:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* Read in data */
options validvarname=v7;
libname NBA xlsx "~/L.E.B.R.O.N. Framework/data/2012_data.xlsx";
*libname NBA clear;

/* Test printing data */
*proc print data=nba.player_usage;
*run;

proc sql;
   create table player_traditional_usage_merged as
   select a.player, a.team, a.age, a.gp, a.w, a.l,
   		a.min, a.pts, a.oreb, a.dreb, a.ast,
   		a.stl, a.blk, b._pts, b._oreb, b._dreb,
   		b._ast, b._stl, b._blk
      from nba.player_traditional a inner join nba.player_usage b
           on a.player = b.player
           and a.team = b.team
           and a.age = b.age
           and a.gp = b.gp
           and a.w = b.w
           and a.l = b.l;
quit;

proc print data=player_traditional_usage_merged;
run;

data player_traditional_usage_merged;
	set player_traditional_usage_merged;
	'Similarity to LeBron'n = ?
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to calculate this Similarity to LeBron column. For this, I would need to subtract all other values from LeBron's values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In R, this would look something like the following:&lt;/P&gt;&lt;PRE&gt;player_traditional_usage_merged$`Similarity to LeBron` &amp;lt;- player_traditional_usage_merged$PTS - player_traditional_usage_merged$PTS[player_traditional_usage_merged$PLAYER == "LeBron James"]&lt;/PRE&gt;&lt;P&gt;What the above R code does is take LeBron's points value and subtract it from every player's points value in the dataset. I am trying to accomplish the same in SAS. Any insight is appreciated, thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 04:47:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939365#M368826</guid>
      <dc:creator>BasketballSAS</dc:creator>
      <dc:date>2024-08-15T04:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939374#M368827</link>
      <description>&lt;P&gt;To achieve the same operation in SAS that you described in R, you can follow a similar approach by first isolating LeBron James' statistics and then subtracting these values from every other player's corresponding values. Here's how you can do this step-by-step:&lt;/P&gt;&lt;P&gt;Step-by-Step Solution&lt;BR /&gt;1. Identify LeBron James' Row: Extract LeBron's statistics and store them in macro variables.&lt;BR /&gt;2. Subtract LeBron's Statistics from All Other Rows: Use a DATA step to subtract these values from each player's statistics.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Step 1: Extract LeBron's statistics into macro variables */&lt;BR /&gt;data _null_;&lt;BR /&gt;set player_traditional_usage_merged;&lt;BR /&gt;if player = "LeBron James" then do;&lt;BR /&gt;call symputx('lebron_pts', pts);&lt;BR /&gt;call symputx('lebron_oreb', oreb);&lt;BR /&gt;call symputx('lebron_dreb', dreb);&lt;BR /&gt;call symputx('lebron_ast', ast);&lt;BR /&gt;call symputx('lebron_stl', stl);&lt;BR /&gt;call symputx('lebron_blk', blk);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* Step 2: Subtract LeBron's statistics from all other players */&lt;BR /&gt;data player_traditional_usage_merged;&lt;BR /&gt;set player_traditional_usage_merged;&lt;BR /&gt;similarity_to_lebron_pts = pts - &amp;amp;lebron_pts;&lt;BR /&gt;similarity_to_lebron_oreb = oreb - &amp;amp;lebron_oreb;&lt;BR /&gt;similarity_to_lebron_dreb = dreb - &amp;amp;lebron_dreb;&lt;BR /&gt;similarity_to_lebron_ast = ast - &amp;amp;lebron_ast;&lt;BR /&gt;similarity_to_lebron_stl = stl - &amp;amp;lebron_stl;&lt;BR /&gt;similarity_to_lebron_blk = blk - &amp;amp;lebron_blk;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* Print the resulting dataset */&lt;BR /&gt;proc print data=player_traditional_usage_merged;&lt;BR /&gt;var player team similarity_to_lebron_pts similarity_to_lebron_oreb similarity_to_lebron_dreb similarity_to_lebron_ast similarity_to_lebron_stl similarity_to_lebron_blk;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Explanation of the Code&lt;BR /&gt;1. Extract LeBron's Statistics:&lt;BR /&gt;The first DATA _null_ step reads through the player_traditional_usage_merged dataset and identifies the row where player = "LeBron James".&lt;BR /&gt;The call symputx function is used to store LeBron's statistics (like pts, oreb, dreb, ast, stl, and blk) in macro variables (e.g., &amp;amp;lebron_pts, &amp;amp;lebron_oreb).&lt;BR /&gt;2. Subtract LeBron's Statistics from All Players:&lt;BR /&gt;The second DATA step iterates through the entire dataset again.&lt;BR /&gt;For each player, it creates new variables (similarity_to_lebron_pts, etc.) that store the difference between the player's stats and LeBron's stats.&lt;BR /&gt;3. Print the Results:&lt;BR /&gt;The proc print step outputs the final dataset showing each player's statistics subtracted from LeBron's statistics&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Feel free to update logs if you get error. I have not tested the code. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 08:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939374#M368827</guid>
      <dc:creator>Mitesh73941</dc:creator>
      <dc:date>2024-08-15T08:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939382#M368831</link>
      <description>&lt;P&gt;A DATA step can do all the calculations, if you keep track of what information is where.&amp;nbsp; I would use arrays for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array lebron {6} _temporary_;
   array stats {6} pts oreb dreb ast stl blk;
   array similar {6} similar_pts similar_oreb simil_dreb similar_ast similar_stl similar_blk;
   if _n_=1 then do;
      set player_traditional_usage_merged (where=(player="LeBron James"));
      lebron{1} = pts;
      lebron{2} = oreb;
      lebron{3} = dreb;
      lebron{4} = ast;
      lebron{5} = stl;
      lebron{6} = blk;
   end;&lt;BR /&gt;   set player_traditional_usage_merged;
   do _n_=1 to 6;
       similar{_n_} = stats{_n_} - lebron{_n_};
   end;
run;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The temporary array cleans up after itself.&amp;nbsp; None of the variables fields get stored permanently once the DATA step ends.&amp;nbsp; You can pick any names you would like in the SIMILAR array.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, but you have the data so easy enough for you to try it.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 09:25:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939382#M368831</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-08-15T09:25:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939384#M368832</link>
      <description>&lt;P&gt;It would be a lot less programming if you use PROC STDIZE with METHOD=IN. Here, in this example, using data set SASHELP.CLASS, we want to subtract the age and height and weight of Jane from all the other records. No macros needed. No arrays needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one_obs;
    set sashelp.class(where=(name='Jane'));
    scale=1;
    keep age height weight scale;
run;

proc stdize data=sashelp.class method=in(one_obs) out=want;
	location age height weight;
	scale scale scale scale;
	var age height weight;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 10:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939384#M368832</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-15T10:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939385#M368833</link>
      <description>&lt;P&gt;Minor variant of &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892" target="_blank" rel="noopener"&gt;PaigeMiller&lt;/A&gt;'s approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Indicate the observation containing the reference values */

data tmp;
set sashelp.class;
_w=(name='James');
run;

/* Perform the subtraction (add a VAR statement to restrict it to selected variables) */

proc stdize data=tmp method=mean out=want(drop=_w);
weight _w;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Aug 2024 10:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939385#M368833</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-08-15T10:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939398#M368842</link>
      <description>&lt;P&gt;That is brilliant,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;. I'm starting to think that SAS is very powerful!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 11:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939398#M368842</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-15T11:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939438#M368856</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;.... I'm starting to think that SAS is very powerful!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I just sprained an abdominal muscle.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 14:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939438#M368856</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-08-15T14:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939546#M368891</link>
      <description>&lt;P&gt;I think it is a SAS/IML thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
use sashelp.class;
read all var _num_ where (name='James') into one_obs;
read all var _num_ into all_obs[c=vname];
read all var _char_ into char_obs[c=vname2];
close;
want=all_obs-one_obs;
create want from char_obs want[c=(vname2||vname)];
append from char_obs want;
close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Aug 2024 08:13:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939546#M368891</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-08-17T08:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract a row from all other rows in data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939670#M368930</link>
      <description>Thank you for all the responses! I have not tested them yet as I am still new to SAS but will be sure to reply to each response, and will be sure to provide the data as a file attachment next time.</description>
      <pubDate>Fri, 16 Aug 2024 17:53:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-a-row-from-all-other-rows-in-data-set/m-p/939670#M368930</guid>
      <dc:creator>BasketballSAS</dc:creator>
      <dc:date>2024-08-16T17:53:02Z</dc:date>
    </item>
  </channel>
</rss>

