<?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 a variable outside of data step based on a variable inside the data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/929997#M365902</link>
    <description>&lt;P&gt;I think you may need to provide some example data sets and the expected result(s).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without an idea of what "two months of data" may look like this is a very imprecise description.&lt;/P&gt;
&lt;P&gt;For example, you show 2 for the value of "months" on three different observations. Do you expect a different result for each observation? If so, how are we to tell which result? Are we to use this information to build a data set name to match with? A library? I really cannot tell how you expect "months" to be used as presented.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In 99.99 percent of cases it is usually better to work with date values instead of arbitrary numbers and calling them "months" (or weeks or quarters or what ever).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 May 2024 21:52:46 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-05-28T21:52:46Z</dc:date>
    <item>
      <title>Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/929994#M365900</link>
      <description>Obs months&lt;BR /&gt;1 2&lt;BR /&gt;2 2&lt;BR /&gt;3 6&lt;BR /&gt;4 2&lt;BR /&gt;&lt;BR /&gt;I want to merge the dataset above with other datasets based on value of months variable. For example, I want to merge two months of data for the first record, two months of data for the second record, 6 months of data for the third record, and 2 months of records for the fourth record. Is there a systematic way of doing it? I was thinking about creating a variable based on the months variable and create a loop within a macro to achieve it.</description>
      <pubDate>Tue, 28 May 2024 21:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/929994#M365900</guid>
      <dc:creator>Yychel</dc:creator>
      <dc:date>2024-05-28T21:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/929997#M365902</link>
      <description>&lt;P&gt;I think you may need to provide some example data sets and the expected result(s).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without an idea of what "two months of data" may look like this is a very imprecise description.&lt;/P&gt;
&lt;P&gt;For example, you show 2 for the value of "months" on three different observations. Do you expect a different result for each observation? If so, how are we to tell which result? Are we to use this information to build a data set name to match with? A library? I really cannot tell how you expect "months" to be used as presented.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In 99.99 percent of cases it is usually better to work with date values instead of arbitrary numbers and calling them "months" (or weeks or quarters or what ever).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2024 21:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/929997#M365902</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-28T21:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930007#M365911</link>
      <description>&lt;P&gt;You need to provide more detail. My advice: Don't jump into any coding (especially macro coding) before you haven't clearly defined what you have and what result you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your thinking: Would you create an output table per row in your driver data or something else. And what's "two months of data"? Are your source tables monthly files (tables) or would you need to sub-set the data. You would certainly need the information your your "driver table" (the 4 rows you shared) with all the information required to identify which source tables and sub-set of data to pick.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 01:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930007#M365911</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-29T01:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930024#M365916</link>
      <description>The main dataset that I share contains promotion information. The months variable refers to the length of the promotion. I want to read merge the main data with sales data. For example, since the length of the promotion for the first record is 2 months, I want to merge that against sales data 1 (sales data in month 1) and sales data 2 (sales data in month 2) with variable obs being the unique id. This would be a simple task if all promotions have the same length. However, the third record has a promotion that lasts 6 months, so I want to read sales data 1, 2, 3, 4, 5, and 6. Is there a way to do it systematically?</description>
      <pubDate>Wed, 29 May 2024 02:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930024#M365916</guid>
      <dc:creator>Yychel</dc:creator>
      <dc:date>2024-05-29T02:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930026#M365917</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466275"&gt;@Yychel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The main dataset that I share contains promotion information. The months variable refers to the length of the promotion. I want to read merge the main data with sales data. For example, since the length of the promotion for the first record is 2 months, I want to merge that against sales data 1 (sales data in month 1) and sales data 2 (sales data in month 2) with variable obs being the unique id. This would be a simple task if all promotions have the same length. However, the third record has a promotion that lasts 6 months, so I want to read sales data 1, 2, 3, 4, 5, and 6. Is there a way to do it systematically?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466275"&gt;@Yychel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is always a way to do things systematically.&lt;/P&gt;
&lt;P&gt;Assuming you're actually after some code example you need to provide some as simple as possible while still representative sample data, explain the logic and show the desired outcome based on the sample data.&lt;/P&gt;
&lt;P&gt;Please provide the sample data via SAS data step code that creates it.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 02:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930026#M365917</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-29T02:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930036#M365923</link>
      <description>Below are my sample codes. I changed value for the second observation in the base dataset for simplicity.&lt;BR /&gt;&lt;BR /&gt;data dataset;&lt;BR /&gt;input id months;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2&lt;BR /&gt;2 1&lt;BR /&gt;3 2&lt;BR /&gt;4 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data sales1;&lt;BR /&gt;input id sales;&lt;BR /&gt;datalines;&lt;BR /&gt;1 100&lt;BR /&gt;2 60&lt;BR /&gt;3 200&lt;BR /&gt;4 250&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data sales2;&lt;BR /&gt;input id sales;&lt;BR /&gt;datalines;&lt;BR /&gt;1 120&lt;BR /&gt;2 90&lt;BR /&gt;3 180&lt;BR /&gt;4 220&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data merge;&lt;BR /&gt;merge dataset (in=a)&lt;BR /&gt;sales1 (rename=(sales=sales_1)&lt;BR /&gt;sales2 (rename=(sales=sales_2);&lt;BR /&gt;by id;&lt;BR /&gt;If a;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Data merge doesn’t work. Since id =2 has months (promo period) =1, I don’t want to read sales from sales2. The idea that I have is to create a variable based on value of “months” from the base data and merge sales data based on it (i.e. merge sales1-sales2 for months = 2)</description>
      <pubDate>Wed, 29 May 2024 03:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930036#M365923</guid>
      <dc:creator>Yychel</dc:creator>
      <dc:date>2024-05-29T03:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930040#M365925</link>
      <description>&lt;P&gt;If I understand right what you want then below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data driver;
  input id months;
  datalines;
1 2
2 1
3 2
4 2
;
run;

data sales1;
  input id sales;
  datalines;
1 100
2 60
3 200
4 250
;
run;

data sales2;
  input id sales;
  datalines;
1 120
2 90
3 180
4 220
;
run;

data all_sales;
  set sales: indsname=_ds;
  months=input(compress(_ds,,'kd'),best32.);
run;

data want(drop=_:);
  set driver;
  if _n_=1 then
    do;
      if 0 then set all_sales(keep=sales);
      dcl hash h1(dataset:'all_sales', multidata:'y');
      h1.defineKey('id','months');
      h1.defineData('sales');
      h1.defineDone();
    end;
  do _i=1 to months;
    if h1.find(key:id, key:_i) ne 0 then call missing(sales);
    output;
  end;
run;
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2024 03:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930040#M365925</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-29T03:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930044#M365927</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466275"&gt;@Yychel&lt;/a&gt;&amp;nbsp;Below two coding options using your sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v_all_sales / view=v_all_sales;
  set sales: indsname=_ds;
  sales_month=input(compress(_ds,,'kd'),best32.);
run;

/* option 1: hash lookup */
data want_1;
  set driver;
  if _n_=1 then
    do;
      if 0 then set v_all_sales(keep=sales);
      dcl hash h1(dataset:'v_all_sales');
      h1.defineKey('id','sales_month');
      h1.defineData('sales');
      h1.defineDone();
    end;
  do sales_month=1 to months;
    if h1.find(key:id, key:sales_month) ne 0 then call missing(sales);
    output;
  end;
run;
/* option 2: SQL join */
proc sql;
  create table want_2 as
  select l.id, l.months, r.sales, r.sales_month
  from driver l
  left join v_all_sales r
    on l.id=r.id 
       and r.sales_month&amp;lt;=l.months
  ;
quit;
proc print data=v_all_sales;
run;
proc print data=want_1;
run;
proc print data=want_2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Please note that with the logic as implemented the SQL left join will not return rows where there is not match to sales data while the hash lookup will. Just delete in your sample data one id in one or both of the sales tables to see the difference between the two approaches.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 04:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930044#M365927</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-29T04:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930138#M365956</link>
      <description>&lt;P&gt;Thanks Patrick. The first option is probably more helpful because in the actual data, month ranges from 10-20 so the second method may be more complicated.&lt;/P&gt;&lt;P&gt;This is the final output that I am looking for so that there's only one observation for each unique id.&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;id months sales_1 sales_2&lt;BR /&gt;1 2 100 120&lt;BR /&gt;2 1 60&lt;BR /&gt;3 2 200 180&lt;BR /&gt;4 2 250 220&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 29 May 2024 14:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930138#M365956</guid>
      <dc:creator>Yychel</dc:creator>
      <dc:date>2024-05-29T14:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930150#M365963</link>
      <description>Use proc transpose on the already created want table</description>
      <pubDate>Wed, 29 May 2024 16:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930150#M365963</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-29T16:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930170#M365969</link>
      <description>&lt;P&gt;Depending on the size of you data you might be able to just convert that dataset into a multilabel format and then use PROC SUMMARY to summarize the data for multiple promotions at once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So assuming you have sales data by MONTH.&amp;nbsp; So data variables like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input month store dept sales;
cards;
1 1 1 100
1 2 1 120
2 1 1 50
2 2 1 300
3 1 1 130
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then make a format that maps the set of months covered by a promotion to the same label.&amp;nbsp; So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value promotion(multilabel);
  1-2 = 'Promotion 1'
  2-5 = 'Promotion 2'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use PROC SUMMARY to produce totals for the different combinations of month values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have completetypes;
  class month / mlf preloadfmt ;
  var sales;
  output out=want sum=sales;
  format month promotion.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs       month       _TYPE_    _FREQ_    sales

 1                       0         5       700
 2     Promotion 1       1         4       570
 3     Promotion 2       1         3       480
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 18:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930170#M365969</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-29T18:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable outside of data step based on a variable inside the data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930190#M365975</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466275"&gt;@Yychel&lt;/a&gt;&amp;nbsp;What Tom proposes using multilabel looks really interesting. You just would need to confirm if it's suitable for your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you've got a table with your promotions then the format is something you don't need to type but you can generate it.&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2024 01:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-outside-of-data-step-based-on-a-variable/m-p/930190#M365975</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-30T01:09:15Z</dc:date>
    </item>
  </channel>
</rss>

