<?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 extract a numeric value from a SAS dataset and use it as a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814511#M321478</link>
    <description>&lt;P&gt;Thanks for a prompt reply!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(1) Each of the separate DATA## sets (I currently have DATA1 through DATA32) evolves differently through time, and merging them would create upstream problems.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(2) In an earlier iteration of the problem I had all the indexes in one file, but I couldn't make that work either and so pulled them apart.&amp;nbsp; It would be easy enough to put them back it that yields a solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(3) I don't want to rename the datasets, I just want to rename some variables in one of the pair of datasets.&amp;nbsp; Both of the pair start out with the same variable names, so one needs to be changed before merging.&amp;nbsp; Once merged, I will calculate some differences between the two datasets for the same original-named variables (and it is relevant which is subtracted from which), then the resulting dataset gets passed along for further analysis.&amp;nbsp; As the datasets evolve through time (see (1)), their role as either the "subtractor" or "subtractee" changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 21 May 2022 00:41:27 GMT</pubDate>
    <dc:creator>JRoteUMN</dc:creator>
    <dc:date>2022-05-21T00:41:27Z</dc:date>
    <item>
      <title>How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814507#M321474</link>
      <description>&lt;P&gt;I have a number of SAS datasets (around 30), each with the same structure but the sets are differentiated by a numerical suffix, e.g., DATA##, where # is some number.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a number of other SAS dataset, e.g., INDICES##, each with one row (observation) of two columns of numerical variables named H and A.&amp;nbsp; This pair of numbers represents the suffixes ## of two of the DATA## datasets that need to be merged for analysis.&amp;nbsp; The number of second datasets is less than 20, and their suffixes are unrelated to those of DATA.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to be able to extract a pair of numbers from each of the second datasets in a way that I can use them to identify each of the pair of datasets from the first set that I need to merge, perhaps renaming them HDATA&lt;EM&gt;XX&lt;/EM&gt; and ADATA&lt;EM&gt;ZZ&lt;/EM&gt;, where XX and ZZ are the relevant identifiers for the comparison.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Several attempts at assigning the value of H or A to a name in a macro then appending that name to DATAxx have failed.&amp;nbsp; One example follows, with 5 suffix-containing datasets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;trial&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%do I = &lt;STRONG&gt;1&lt;/STRONG&gt; %to &lt;STRONG&gt;5&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;data temp; set INDICES&amp;amp;I;&lt;/P&gt;&lt;P&gt;%let key=A; run;&lt;/P&gt;&lt;P&gt;data DATA&amp;amp;key; set DATA&amp;amp;key;&lt;/P&gt;&lt;P&gt;rename coreoff = coredef;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt; trial;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;trial&lt;/EM&gt;&lt;/STRONG&gt;; &lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;In this case, SAS interprets “DATA&amp;amp;key” as “DATAA”, which doesn’t exist.&amp;nbsp; Any help appreciated.&lt;/P&gt;&lt;P&gt;SAS Ver. 9.4 running in Windows 10.&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2022 23:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814507#M321474</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-20T23:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814509#M321476</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Why do you have separate datasets?&lt;/P&gt;
&lt;P&gt;Why is the number part of the dataset name instead of variable in the dataset?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_data;
  set data: indsname=dsname;
  dsnum = input(substr(scan(dsname,-1,'.'),5),32.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why do you have separate index dataset?&amp;nbsp; Why not combine those into one also?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to rename the datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you planning to do once you have renamed the datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 00:11:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814509#M321476</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-21T00:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814510#M321477</link>
      <description>&lt;P&gt;It is doing what you asked it to do.&amp;nbsp; The macro processor finishes its work and passes the resulting text to SAS to actually run. Any macro code embedded inside of SAS code is going to be evaluated before SAS even starts to compile the code, much less begins to run it. So placing a %LET statement in the middle of data step is just going to confuse the humans reading the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You effectively ran these statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let key=A;
data temp;
  set INDICES&amp;amp;I;
run;
data DATA&amp;amp;key; 
  set DATA&amp;amp;key;
  rename coreoff = coredef;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make a macro variable from the value of a dataset variable you use the CALL SYMPUTX() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set INDICES&amp;amp;I;
  call symputx('key',a);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that said your macro does not look like it is trying to do what you described.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I don't understand what value it would be to do what you described.&amp;nbsp; Why are you renaming datasets? What are going to do with the datasets once they are renamed that you couldn't do with them using their original names?&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 00:37:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814510#M321477</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-21T00:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814511#M321478</link>
      <description>&lt;P&gt;Thanks for a prompt reply!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(1) Each of the separate DATA## sets (I currently have DATA1 through DATA32) evolves differently through time, and merging them would create upstream problems.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(2) In an earlier iteration of the problem I had all the indexes in one file, but I couldn't make that work either and so pulled them apart.&amp;nbsp; It would be easy enough to put them back it that yields a solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(3) I don't want to rename the datasets, I just want to rename some variables in one of the pair of datasets.&amp;nbsp; Both of the pair start out with the same variable names, so one needs to be changed before merging.&amp;nbsp; Once merged, I will calculate some differences between the two datasets for the same original-named variables (and it is relevant which is subtracted from which), then the resulting dataset gets passed along for further analysis.&amp;nbsp; As the datasets evolve through time (see (1)), their role as either the "subtractor" or "subtractee" changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 00:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814511#M321478</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-21T00:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814513#M321479</link>
      <description>&lt;P&gt;Alas, I'm still relatively new to macros, and indeed I was a confused human reading (or writing) the code.&amp;nbsp; I will try to implement your suggestion.&amp;nbsp; And sorry for the lack of clarity - I'm not renaming datasets (or at least that's not my intention), I'm just renaming some variables within one of the pair of datasets.&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 00:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814513#M321479</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-21T00:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814515#M321481</link>
      <description>&lt;P&gt;You still haven't said enough about what you are trying to do to give any good advice.&lt;/P&gt;
&lt;P&gt;There is no need to rename the variables IN THE EXISTING datasets.&amp;nbsp; Rename them as part of the MERGE (what ever you mean by that) using the RENAME= dataset option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say you want to merge DATA34 and DATA87 by the key variable ID and create different names for non-key common variable&amp;nbsp; named FRED.&amp;nbsp; Your code might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   merge data34(rename=(FRED=LEFT)) data87(rename=(FRED=RIGHT));
   by id;
   difference = left - right;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So all that changes in that is the 34 and 87.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So make two macro variable to hold those numbers and replace the hard coded numbers with the macro variable references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let left=34;
%let right=87;
data want;
   merge data&amp;amp;left.(rename=(FRED=LEFT)) data&amp;amp;right.(rename=(FRED=RIGHT));
   by id;
   difference = left - right;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you could convert this into a macro with two input parameters. And call it with the values you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro combine(left,right);
data want;
   merge data&amp;amp;left.(rename=(FRED=LEFT)) data&amp;amp;right.(rename=(FRED=RIGHT));
   by id;
   difference = left - right;
run;
%mend combine ;

%combine(left=34,right=87)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if you have a dataset named INDEX with the variable LEFT and RIGHT you can use it to generate one macro call for each observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set index;
  call execute(cats('%nrstr(%combine)(left=',left,',right=',right,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Obviously your real macro will do more steps than just the one I have shown.&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 01:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814515#M321481</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-21T01:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814516#M321482</link>
      <description>Thanks! I gotta go now, but will give this a try tomorrow.</description>
      <pubDate>Sat, 21 May 2022 01:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814516#M321482</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-21T01:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814517#M321483</link>
      <description>&lt;P&gt;In general, it's a good idea to show the non-macro code that works before the macro code. Once you have working code, then it's usually easy to convert it to a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 May 2022 01:04:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814517#M321483</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-21T01:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814586#M321531</link>
      <description>&lt;P&gt;That did it.&amp;nbsp; "Symputx" was missing from my still limited set of macro statements.&amp;nbsp; My original macro for this problem was essentially what you showed in another reply, starting by setting each of the indexes (LEFT and RIGHT in your example) "by hand."&amp;nbsp; As the number of combinations grew large, this became onerous, leading to trying to extract the relevant indices from files generated elsewhere in the program.&amp;nbsp; Thanks also for advice in another post on renaming variables while merging; that streamlines code in several downstream locations.&amp;nbsp; Very much obliged for you patience and assistance.&lt;/P&gt;</description>
      <pubDate>Sun, 22 May 2022 00:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814586#M321531</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-22T00:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814888#M321655</link>
      <description>You should mark Tom's solution as the correct answer.</description>
      <pubDate>Tue, 24 May 2022 15:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/814888#M321655</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-24T15:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815181#M321753</link>
      <description>Sorry; I'm a new user. I thought I had marked Tom's solution as correct&lt;BR /&gt;but now I'm not sure if my attribution was correct. At this point I can't&lt;BR /&gt;find a proper button to push to recognize his contribution.&lt;BR /&gt;</description>
      <pubDate>Thu, 26 May 2022 08:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815181#M321753</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-26T08:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815184#M321754</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/425813"&gt;@JRoteUMN&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/425813"&gt;@JRoteUMN&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;At this point I can't find a proper button to push to recognize his contribution.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Normally you can select a different post&amp;nbsp;as the solution after clicking&amp;nbsp;"Not the Solution" in the option menu (see icon below) of the current solution.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="show_option_menu.png" style="width: 155px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71789i04C9FD720BF1EEC0/image-size/large?v=v2&amp;amp;px=999" role="button" title="show_option_menu.png" alt="show_option_menu.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 08:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815184#M321754</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-26T08:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a numeric value from a SAS dataset and use it as a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815185#M321755</link>
      <description>&lt;P&gt;Got it.&amp;nbsp; Thanks.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 09:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-numeric-value-from-a-SAS-dataset-and-use-it-as/m-p/815185#M321755</guid>
      <dc:creator>JRoteUMN</dc:creator>
      <dc:date>2022-05-26T09:07:54Z</dc:date>
    </item>
  </channel>
</rss>

