<?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: Macro to transverse list of variable names from dictionary.columns, then perform action on data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876730#M346348</link>
    <description>&lt;P&gt;What SAS code do you want to use the macro to generate?&lt;/P&gt;
&lt;P&gt;First step is to write the code you want it to generate and then you can start to plan how you can have the macro generate it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you said:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;What I want is to assign DIAG to the CONTENTS of the variable "Diagnosiscode1" in data set Medical,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Does that mean you want to assign the actual SAS variable named DIAG to the value of the actual SAS variable named DIAGNOSISCODE1 ?&amp;nbsp; The SAS code for that would look like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag = diagnosiscode1 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;So if you had the string diagnosiscode1 in the macro variable named DIAGVAR you could use it to generate that code in this way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag = &amp;amp;diagvar ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Of course you would have to place that assignment statement inside of a data step.&amp;nbsp; So if DIAGNOSISCODE1 is a variable in the dataset MEDICAL the SAS code might look like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set medical;
  diag = &amp;amp;diagvar ;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2023 22:18:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-19T22:18:31Z</dc:date>
    <item>
      <title>Macro to transverse list of variable names from dictionary.columns, then perform action on data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876729#M346347</link>
      <description>&lt;P&gt;Creating a macro to first select all column_names from a data set&amp;nbsp; into a list "dcodes",&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then cycle through column_names, and if the column_name starts with "Diagnosiscode" (three such columns - Diagnosiscode1, Diagnosiscode2, Diagnosiscode3) apply simple logic based on the CONTENTS of the variable.&amp;nbsp; If the contents of var Diagnosiscode1 is between the string "DC_101" and "DX_900" then perform some action (add a flag to another, different variable not in the :dcodes list&amp;nbsp; - ("DX_GP01").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The log below clearly shows macro variable DIAG resolves to diagnosiscode1&lt;STRONG&gt;, &lt;/STRONG&gt;yet the mlogic resolves to false.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;When&amp;nbsp; DIAG resolves to "Diagnosiscode1", or "Diagnosiscode2" or "Diagnosiscode3", the next step will fail -line 11 - (logically),&amp;nbsp; as the string "Diagnosiscode1" is of course, NOT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ge&amp;nbsp;'DX_515' and le 'DX_910'.&amp;nbsp; &amp;nbsp;What I want is to assign DIAG to the CONTENTS of the variable "Diagnosiscode1" in data set Medical, so the logic would work if the data in that variable was in fact,&lt;/P&gt;
&lt;P&gt;ge&amp;nbsp;'DX_515' and le 'DX_910'.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So there are two issues:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Why doesn't the logic in line 10 work&amp;nbsp; the attached log.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Once the logic does work, how to change macro variable DIAG from line 8 so the CONTENTS of&amp;nbsp; the name of that variable can be scanned for certain conditions, and if true, apply the logic to another variable.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simpler way would be to just loop through the variables Diagnosiscode1, Diagnosiscode2 &amp;amp; Diagnosiscode3,&amp;nbsp; one row at a time, and modify the contents of a second variable based on what was found.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are many examples of looping through a SAS data set here, but the the output is always to another data set.&amp;nbsp; I want to change values in the same data set.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;THX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;1 %macro sum_flg();&lt;BR /&gt;2 data med_grp;&lt;BR /&gt;3 length dx_GP01 2.;&lt;BR /&gt;4 set medical;&lt;BR /&gt;5 %local i diag;&lt;BR /&gt;6 %let i=1;&lt;BR /&gt;7 %do %while (%scan(&amp;amp;dcodes, &amp;amp;i) ne );&lt;BR /&gt;8 %let diag = %scan(&amp;amp;dcodes, &amp;amp;i);&lt;BR /&gt;9 %put &amp;amp;diag.;&lt;BR /&gt;10 %if &amp;amp;diag. = ('diagnosiscode%') %then %do;&lt;BR /&gt;11 %if &amp;amp;diag. ge 'DX_515' and &amp;amp;diag. LE 'DX_910' %then %let Dx_GP01 = '1';&lt;BR /&gt;12 %ELSE Dx_GP01 = '0';&lt;BR /&gt;13 %end;&lt;BR /&gt;14 %let i = %eval(&amp;amp;i + 1);&lt;BR /&gt;15 %end;&lt;BR /&gt;16 %mend sum_flg&lt;/PRE&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;
&lt;PRE&gt;SYMBOLGEN:  Macro variable DIAG resolves to &lt;STRONG&gt;diagnosiscode1&lt;/STRONG&gt;
MLOGIC(SUM_FLG):  %IF condition &amp;amp;diag. = ('diagnosiscode%') is FALSE&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 21:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876729#M346347</guid>
      <dc:creator>Jumboshrimps</dc:creator>
      <dc:date>2023-05-19T21:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to transverse list of variable names from dictionary.columns, then perform action on data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876730#M346348</link>
      <description>&lt;P&gt;What SAS code do you want to use the macro to generate?&lt;/P&gt;
&lt;P&gt;First step is to write the code you want it to generate and then you can start to plan how you can have the macro generate it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you said:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;What I want is to assign DIAG to the CONTENTS of the variable "Diagnosiscode1" in data set Medical,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Does that mean you want to assign the actual SAS variable named DIAG to the value of the actual SAS variable named DIAGNOSISCODE1 ?&amp;nbsp; The SAS code for that would look like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag = diagnosiscode1 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;So if you had the string diagnosiscode1 in the macro variable named DIAGVAR you could use it to generate that code in this way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag = &amp;amp;diagvar ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Of course you would have to place that assignment statement inside of a data step.&amp;nbsp; So if DIAGNOSISCODE1 is a variable in the dataset MEDICAL the SAS code might look like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set medical;
  diag = &amp;amp;diagvar ;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 22:18:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876730#M346348</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-19T22:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to transverse list of variable names from dictionary.columns, then perform action on data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876737#M346351</link>
      <description>&lt;P&gt;This problem:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;if the column_name starts with "Diagnosiscode" (three such columns - Diagnosiscode1, Diagnosiscode2, Diagnosiscode3) apply simple logic based on the CONTENTS of the variable.&amp;nbsp; If the contents of var Diagnosiscode1 is between the string "DC_101" and "DX_900" then perform some action (add a flag to another, different variable not in the :dcodes list&amp;nbsp; - ("DX_GP01").&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Does not need a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array dxcodes diagnosiscode: ;
  dx_gp01 = 0;
  do index=1 to dim(dxcodes) until(dx_gp01);
    dx_gp01 = 'DC-101' &amp;lt;= dxcodes[index] &amp;lt;= 'DX_900' ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 May 2023 01:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876737#M346351</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-20T01:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to transverse list of variable names from dictionary.columns, then perform action on data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876740#M346353</link>
      <description>&lt;P&gt;This is a classic example of where a long data table (diagnoses in rows) is way better than your current wide table (diagnoses in columns) and will make your processing a lot easier. If you want to try this than please provide us with some sample data using the DATA step DATALINES statement.&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2023 04:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-transverse-list-of-variable-names-from-dictionary/m-p/876740#M346353</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-05-20T04:28:20Z</dc:date>
    </item>
  </channel>
</rss>

