<?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: Create variable and assign weighted values using vlookup in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854643#M337782</link>
    <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2023 18:04:07 GMT</pubDate>
    <dc:creator>fastandcurious</dc:creator>
    <dc:date>2023-01-19T18:04:07Z</dc:date>
    <item>
      <title>Create variable and assign weighted values using vlookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/852842#M337092</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Hello, beginner posting for the 1st time,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I want to create a new column that assigns weighted values to a large dataset using a vlookup table in SAS, rather than write hundreds of &lt;EM&gt;if-then&lt;/EM&gt; statements. For example, I have LARGE dataset and each record has a unique groupid that specifies the stratified group it belongs to. (I have hundreds of groups and weighted values)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Vlookup table example:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fastandcurious_0-1673279787756.png" style="width: 310px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79187iD0C8D13CB50C745D/image-dimensions/310x316?v=v2" width="310" height="316" role="button" title="fastandcurious_0-1673279787756.png" alt="fastandcurious_0-1673279787756.png" /&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;How can I create a new column where the weighted value is returned based off the string? (Example: If Unique ID=221 then Weight=17.49)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Desired output:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fastandcurious_2-1673279069957.png" style="width: 228px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79186iFB4F9D9661BBB558/image-dimensions/228x204?v=v2" width="228" height="204" role="button" title="fastandcurious_2-1673279069957.png" alt="fastandcurious_2-1673279069957.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could use excel for this but I want to use SAS and improve my programming skills. Would array be the best function? What are other ways to do this in SAS? Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/852842#M337092</guid>
      <dc:creator>fastandcurious</dc:creator>
      <dc:date>2023-01-09T16:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable and assign weighted values using vlookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/852846#M337094</link>
      <description>&lt;P&gt;Here's a good paper that illustrates quite a few methods.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2219-2015.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings15/2219-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Join (MERGE or SQL)&lt;/LI&gt;
&lt;LI&gt;Format&lt;/LI&gt;
&lt;LI&gt;Arrays&lt;/LI&gt;
&lt;LI&gt;Key Indexing&lt;/LI&gt;
&lt;LI&gt;Hash Table&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;For the start, Joins, Formats and Arrays can get you 99% of the way there. I've used those three almost exclusively for my entire SAS programming career.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/852846#M337094</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-01-09T16:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable and assign weighted values using vlookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854643#M337782</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2023 18:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854643#M337782</guid>
      <dc:creator>fastandcurious</dc:creator>
      <dc:date>2023-01-19T18:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable and assign weighted values using vlookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854665#M337795</link>
      <description>&lt;P&gt;The BEST way depends on a lot on things like the size of two datasets and the types of the variables being created.&lt;/P&gt;
&lt;P&gt;The most natural SAS way is to use a FORMAT to translate a code into a string.&amp;nbsp; If you need to create a numeric variable, like your WEIGHT example, then you will need to use an INFORMAT instead. But an INFORMAT needs a character value as the input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another natural way is to just to combine the two datasets.&amp;nbsp; Use a JOIN with PROC SQL.&amp;nbsp; Or MERGE using a data step.&amp;nbsp; Both of those require sorting by the key variable (but the SQL method will do the sorting for you) so if the datasets are large and not already sorted by the key variable that can be a performance issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A newer method in SAS is a HASH object. But that requires that the lookup table is small enough to fit into memory.&amp;nbsp; it does not require the other (potentially much larger dataset) to be sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data group_wt;
  input group_id $ weight ;
cards;
111 42.38
121 14.32
;

data have;
  input id $ group_id $ date :date.;
  format date date9.;
cards;
1 121 01JAN2022
2 100 01FEB2022
3 111 01MAR2023
;

data want;
  set have;
  if _n_=1 then do;
    if 0 then set group_wt(keep=group_id weight);
    declare hash vlookup(dataset:"group_wt(keep=group_id weight");
    _error_ = vlookup.definekey("group_id");
    _error_ = vlookup.definedata(all:'yes');
    _error_ = vlookup.definedone();
  end;
  if vlookup.find() then call missing(of weight);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2023 19:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854665#M337795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-19T19:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable and assign weighted values using vlookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854835#M337862</link>
      <description>&lt;P&gt;Thank you so much! This is helpful.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jan 2023 14:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-and-assign-weighted-values-using-vlookup/m-p/854835#M337862</guid>
      <dc:creator>fastandcurious</dc:creator>
      <dc:date>2023-01-20T14:15:40Z</dc:date>
    </item>
  </channel>
</rss>

