<?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 execute large chunks of code from a variable in a single data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638354#M189830</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You might try a user-defined format. Here's a simple example. If you needed to you could make the user-defined format from your list in a program. This example doesn't have that, but it should give you an idea:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data fakedata;
   infile datalines dlm=',';
   input ID $ name $ amt;
datalines;
abc,alan,100
cde,barb,200
efg,carl,300
fgh,edna,400
abg,fred,500
abc,gail,600
;
run;

proc format;
  value $change 'abc','cde'='abc/cde' 
                'efg' = 'efg/xxx';
run;

data new;
  length ID orig_ID$10;
  set fakedata;
  orig_id = id;
  ID = put(orig_id,$change.);
run;

proc print data=new;
  var ID name amt orig_ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;any values NOT in the user-defined format would retain their original values, as shown in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Wed, 08 Apr 2020 16:30:21 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2020-04-08T16:30:21Z</dc:date>
    <item>
      <title>How to execute large chunks of code from a variable in a single data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638352#M189829</link>
      <description>&lt;P&gt;I have a large dataset, '&lt;EM&gt;have&lt;/EM&gt;'&amp;nbsp;(500 million rows), which I need to make some adjustments to before I run my analysis on it. The data set I have has an 'customer_id' variable that needs to be modified based on values from an external Excel file (has about 5000 rows). This Excel dataset currently looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;customer_id1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;customer_id2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;combined&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;cde&lt;/TD&gt;&lt;TD&gt;abc/cde&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;bbb&lt;/TD&gt;&lt;TD&gt;bdc&lt;/TD&gt;&lt;TD&gt;bbb/bdc&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, using this table, I want to run the following code in a single data step to modify the dataset '&lt;EM&gt;have&lt;/EM&gt;' like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    if customer_id in ("abc", "cde") then customer_id = "abc/cde";
    /*repeat this line 5000 times with all the values in the Excel file*/
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I was previously adding the entire "if-then" string as a variable in the Excel file and putting in to a macro variable using a select-into but unfortunately there's a length restriction on macro variables. I was wondering if there was a different way for me to print all 5000 if-then clauses in a single data step? I don't want to use a merge here.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 16:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638352#M189829</guid>
      <dc:creator>Ani7</dc:creator>
      <dc:date>2020-04-08T16:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute large chunks of code from a variable in a single data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638354#M189830</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You might try a user-defined format. Here's a simple example. If you needed to you could make the user-defined format from your list in a program. This example doesn't have that, but it should give you an idea:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data fakedata;
   infile datalines dlm=',';
   input ID $ name $ amt;
datalines;
abc,alan,100
cde,barb,200
efg,carl,300
fgh,edna,400
abg,fred,500
abc,gail,600
;
run;

proc format;
  value $change 'abc','cde'='abc/cde' 
                'efg' = 'efg/xxx';
run;

data new;
  length ID orig_ID$10;
  set fakedata;
  orig_id = id;
  ID = put(orig_id,$change.);
run;

proc print data=new;
  var ID name amt orig_ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;any values NOT in the user-defined format would retain their original values, as shown in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 16:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638354#M189830</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-04-08T16:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute large chunks of code from a variable in a single data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638360#M189832</link>
      <description>&lt;P&gt;Looks like you just need a format.&amp;nbsp; With a format there is no need to actually change the data, just apply the format when you want to see the formatted values instead of the raw values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make a format from your data just convert it to a dataset in the format needed for a format definition using the CNTLIN= option of PROC FORMAT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length customer_id1 customer_id2 $20 combined $50 ;
  input customer_id1 customer_id2 combined;
cards;
abc cde abc/cde
bbb bdc bbb/bdc
;

data format;
  set have ;
  retain fmtname '$combined';
  start=customer_id1;
  label=combined;
  output;
  start=customer_id2;
  output;
  keep fmtname start label;
run;

proc format cntlin=format;
run;

proc print data=have;
  format customer_id: $combined.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       customer_    customer_
Obs       id1          id2       combined

 1      abc/cde      abc/cde     abc/cde
 2      bbb/bdc      bbb/bdc     bbb/bdc
&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Apr 2020 16:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638360#M189832</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-08T16:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute large chunks of code from a variable in a single data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638398#M189836</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223871"&gt;@Ani7&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why don't you want to use merge (or sql join, which amounts to the same)? - it is not only simpler and easier to write, it will run much faster. 500 million rows x 5000 if-thens sounds to me like a never-ending job&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a SAS license you have already bought the world's best tools for this type of data manipulation, so why try to build your own out of odds and ends? - I am really curious.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 17:10:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638398#M189836</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-04-08T17:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute large chunks of code from a variable in a single data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638491#M189868</link>
      <description>&lt;P&gt;Look into using hash tables. They are much faster than the other ways of doing this.&amp;nbsp; &amp;nbsp;Google it!&amp;nbsp; &amp;nbsp;&lt;BR /&gt;First read the excel sheet into a SAS table. If the example reflects the logic completely, you &lt;BR /&gt;would want to split each row into 2 lines&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;customer_id1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;customer_id2&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;combined&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;abc&lt;/TD&gt;
&lt;TD&gt;cde&lt;/TD&gt;
&lt;TD&gt;abc/cde&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;bbb&lt;/TD&gt;
&lt;TD&gt;bdc&lt;/TD&gt;
&lt;TD&gt;bbb/bdc&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;becomes a sas table called&amp;nbsp;&amp;nbsp;customer_lookup like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="194px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="107px" height="30px"&gt;&lt;STRONG&gt;customer_id&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="87px" height="30px"&gt;&lt;STRONG&gt;combined&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="107px" height="30px"&gt;abc&lt;/TD&gt;
&lt;TD width="87px" height="30px"&gt;abc/cde&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="107px" height="30px"&gt;cde&lt;/TD&gt;
&lt;TD width="87px" height="30px"&gt;abc/cde&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="107px" height="30px"&gt;bbb&lt;/TD&gt;
&lt;TD width="87px" height="30px"&gt;bbb/bdc&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;bdc&lt;/TD&gt;
&lt;TD&gt;bbb/bdc&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* this uses a macro that simplifies the code to define a hash table&amp;nbsp;In your case, maybe good to look up the syntax! */&lt;/P&gt;
&lt;P&gt;/* If interested, I can post the macro &lt;CODE class=" language-sas"&gt;ut_hash_define &lt;/CODE&gt;*/&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;

   %ut_hash_define(intable=customer_lookup,
     keys=customer_id,
     datacols=combined);

rc = customer_lookup.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Apr 2020 20:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-execute-large-chunks-of-code-from-a-variable-in-a-single/m-p/638491#M189868</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-04-08T20:53:37Z</dc:date>
    </item>
  </channel>
</rss>

