<?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: Most efficient way to use long list of alphanumeric values to create new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955837#M373269</link>
    <description>&lt;P&gt;You could make a informat by proc format or Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data company_groups;
  input start $ label ;
  retain fmtname 'fmt' type 'i' ;
cards;
123abc 1
abc234 1
789ghi 1
222xyz 2
444qrs 2
432opm 2
;
proc format cntlin=company_groups;
run;




data have;
  input oldvar $ ;
cards;
123abc 
abc234 
789ghi 
222xyz 
444qrs 
432opm 
;
data want;
 set have;
 newvar=input(oldvar,fmt.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 12 Jan 2025 07:23:30 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2025-01-12T07:23:30Z</dc:date>
    <item>
      <title>Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955833#M373266</link>
      <description>&lt;P&gt;I have a date set with oldvar comprised of a few thousand alphanumeric values. Big chunks of these values (60-100) represent respective organizations. I want to create newvar with a value based on the value of oldvar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a similar situation with only a few values for a variable I have done the below.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
     set have;
     If oldvar in ("123abc" "abc234" "789ghi") then newvar=1;
     else if oldvar in ("222xyz" "444qrs" "432opm") then newvar=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case&amp;nbsp;with a few thousand&amp;nbsp; values I am trying to paste from an excel file, what is the most efficient way to do this without having to put quotation marks around every single value? Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 05:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955833#M373266</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-01-12T05:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955834#M373267</link>
      <description>&lt;P&gt;Leave the list in DATA, not CODE.&lt;/P&gt;
&lt;P&gt;So make a dataset that has OLDVAR and NEWVAR&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data company_groups;
  input oldvar $ newvar ;
cards;
123abc 1
abc234 1
789ghi 1
222xyz 2
444qrs 2
432opm 2
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then merge it with HAVE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have(in=in1) company_groups;
  by oldvar;
  if in1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Jan 2025 05:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955834#M373267</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-12T05:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955835#M373268</link>
      <description>&lt;P&gt;Thank you, very helpful. I needed one more step and had one additional question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step: Sort by oldvar&lt;/P&gt;
&lt;P&gt;*I received an error&amp;nbsp;message because I hadn't sorted by the "by" variable in each data set. This was an easy fix by sorting each of the data sets by oldvar.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question:&lt;/P&gt;
&lt;P&gt;When I used your code with merge, the number of observations increased. Does this mean that there must be some "oldvar" in the "company_groups" that were not in the "have" dataset?&amp;nbsp; Is there an easy way to test for this if many observations in the "have" dataset have a blank or missing value for oldvar?&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 06:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955835#M373268</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-01-12T06:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955837#M373269</link>
      <description>&lt;P&gt;You could make a informat by proc format or Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data company_groups;
  input start $ label ;
  retain fmtname 'fmt' type 'i' ;
cards;
123abc 1
abc234 1
789ghi 1
222xyz 2
444qrs 2
432opm 2
;
proc format cntlin=company_groups;
run;




data have;
  input oldvar $ ;
cards;
123abc 
abc234 
789ghi 
222xyz 
444qrs 
432opm 
;
data want;
 set have;
 newvar=input(oldvar,fmt.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Jan 2025 07:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955837#M373269</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-12T07:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955839#M373271</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In this case&amp;nbsp;with a few thousand&amp;nbsp; values I am trying to paste from an excel file, what is the most efficient way to do this without having to put quotation marks around every single value? Thank you.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See this explanation of using the %QLIST macro for avoiding adding quotation marks. &lt;A href="https://communities.sas.com/t5/SAS-Programming/Assign-the-values-to-a-macro-variable/m-p/955804#M373256" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Assign-the-values-to-a-macro-variable/m-p/955804#M373256&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; said, put these values in a data set rather than code.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 12:27:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955839#M373271</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-12T12:27:25Z</dc:date>
    </item>
    <item>
      <title>Re: Most efficient way to use long list of alphanumeric values to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955846#M373275</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, very helpful. I needed one more step and had one additional question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step: Sort by oldvar&lt;/P&gt;
&lt;P&gt;*I received an error&amp;nbsp;message because I hadn't sorted by the "by" variable in each data set. This was an easy fix by sorting each of the data sets by oldvar.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question:&lt;/P&gt;
&lt;P&gt;When I used your code with merge, the number of observations increased. Does this mean that there must be some "oldvar" in the "company_groups" that were not in the "have" dataset?&amp;nbsp; Is there an easy way to test for this if many observations in the "have" dataset have a blank or missing value for oldvar?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes. To use MERGE the datasets must be sorted (or have an index that can be used to retrieve the values) in the order specified in the BY statement.&amp;nbsp; You could use an SQL join instead, in which case SQL will sort the data for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the number of observations increases there are two possible causes.&amp;nbsp; Like you said it could be that some extra observations were added.&amp;nbsp; But the subsetting IF in my code eliminates those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have(in=in1) company_groups;
  by oldvar;
  if in1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The other reason would be that you have multiple observations with the same value of OLDVAR. Either just plain duplicate observations.&amp;nbsp; Or OLDVAR values that are mapped to more than one NEWVAR value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could fix that by first eliminating the duplicates using PROC SORT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=company_groups nodupkey;
  by oldvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Jan 2025 16:25:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-efficient-way-to-use-long-list-of-alphanumeric-values-to/m-p/955846#M373275</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-12T16:25:16Z</dc:date>
    </item>
  </channel>
</rss>

