<?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: embedding macro in data step  (involving array) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938992#M368760</link>
    <description>&lt;P&gt;If you are having trouble visualizing what CODE the macro will generate use the MPRINT option and you SEE it in the LOG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That macro will first run a PROC step, then a DATA step.&amp;nbsp; Then it will EMIT the values of a series of macro variables.&amp;nbsp; So if the values of variable named DISEASE in the dataset named DISEASES has valid SAS code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diseases;
  disease='data x; set y; run;' ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your macro makes some sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if instead DISEASE just have values like: A21, B54 then it makes no sense to have the macro generate code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort; .....
data ....
A21;
B54;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SAS language complier is not going to understand those last two statements.&lt;/P&gt;</description>
    <pubDate>Mon, 12 Aug 2024 17:15:24 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-12T17:15:24Z</dc:date>
    <item>
      <title>embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938700#M368678</link>
      <description>&lt;P&gt;OK I have a macro that does work below&amp;nbsp; however I am looking to execute&amp;nbsp; a macro within a data step as if this works-there will be other&amp;nbsp; computations&lt;/P&gt;
&lt;P&gt;This macro will designate if a patient has a disease based on ICD 10 code groupings -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance appreciated. TIA&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data fake_patients;
    length subject $5 ICD1-ICD10 $7;
    array icd_codes[10] $ ICD1-ICD10;

    /* List of ICD-10 codes for various diseases */
    array pneumonia_codes[10] $7 _temporary_ ('J12.0', 'J12.1', 'J12.2', 'J12.3', 'J12.4', 
                                              'J12.5', 'J12.8', 'J12.9', 'J13', 'J14');
    array diabetes_codes[8] $7 _temporary_ ('E10.9', 'E11.9', 'E13.9', 'E14.9', 
                                            'E10.65', 'E11.65', 'E13.65', 'E14.65');
    array hypertension_codes[10] $7 _temporary_ ('I10', 'I11.0', 'I11.9', 'I12.0', 
                                                 'I12.9', 'I13.0', 'I13.10', 'I13.11', 
                                                 'I13.2', 'I15.0');
    array asthma_codes[10] $7 _temporary_ ('J45.20', 'J45.21', 'J45.22', 'J45.30', 
                                           'J45.31', 'J45.32', 'J45.40', 'J45.41', 
                                           'J45.42', 'J45.50');
    array other_codes[10] $7 _temporary_ ('A00', 'A01', 'A02', 'A03', 'A04', 
                                          'A05', 'A06', 'A07', 'A08', 'A09');
    
    /* Create 50 fake patients */
    do subject_id = 1 to 50;
        subject = cats('S', put(subject_id, z3.));

        /* Assign random ICD-10 codes to each patient */
        do i = 1 to 10;
            /* Randomly choose a disease category */
            disease_category = rand('integer', 1, 5);
            
            select (disease_category);
                when (1) icd_codes[i] = pneumonia_codes[rand('integer', 1, dim(pneumonia_codes))];
                when (2) icd_codes[i] = diabetes_codes[rand('integer', 1, dim(diabetes_codes))];
                when (3) icd_codes[i] = hypertension_codes[rand('integer', 1, dim(hypertension_codes))];
                when (4) icd_codes[i] = asthma_codes[rand('integer', 1, dim(asthma_codes))];
                when (5) icd_codes[i] = other_codes[rand('integer', 1, dim(other_codes))];
            end;
        end;

        output;
    end;
run;




data diseases;
    length disease $20 DX1-DX10 $7;

    /* Pneumonia and its ICD-10 codes */
    disease = "Pneumonia";
    DX1 = 'J12.0'; DX2 = 'J12.1'; DX3 = 'J12.2'; DX4 = 'J12.3';
    DX5 = 'J12.4'; DX6 = 'J12.5'; DX7 = 'J12.8'; DX8 = 'J12.9';
    DX9 = 'J13'; DX10 = 'J14';
    output;

    /* Diabetes and its ICD-10 codes */
    disease = "Diabetes";
    DX1 = 'E10.9'; DX2 = 'E11.9'; DX3 = 'E13.9'; DX4 = 'E14.9';
    DX5 = 'E10.65'; DX6 = 'E11.65'; DX7 = 'E13.65'; DX8 = 'E14.65';
    DX9 = ''; DX10 = '';
    output;

    /* Hypertension and its ICD-10 codes */
    disease = "Hypertension";
    DX1 = 'I10'; DX2 = 'I11.0'; DX3 = 'I11.9'; DX4 = 'I12.0';
    DX5 = 'I12.9'; DX6 = 'I13.0'; DX7 = 'I13.10'; DX8 = 'I13.11';
    DX9 = 'I13.2'; DX10 = 'I15.0';
    output;

    /* Asthma and its ICD-10 codes */
    disease = "Asthma";
    DX1 = 'J45.20'; DX2 = 'J45.21'; DX3 = 'J45.22'; DX4 = 'J45.30';
    DX5 = 'J45.31'; DX6 = 'J45.32'; DX7 = 'J45.40'; DX8 = 'J45.41';
    DX9 = 'J45.42'; DX10 = 'J45.50';
    output;
run;


%macro sendR2D2(data=, var=);
   proc sort data=&amp;amp;data(keep=&amp;amp;var) out=values nodupkey;
       by &amp;amp;var;
   run;

   data _null_;
      set values end=last;
      call symputx('DIS'||left(_n_), &amp;amp;var);
      if last then call symputx('countx', _n_, 'g');
   run;

%put _local_;

%do i=1 %to &amp;amp;countx;

proc sql noprint;
    select  catx(',', compress("'"||DX1||"'"), compress("'"||DX2||"'"), compress("'"||DX3||"'"), 
compress("'"||DX4||"'"),compress("'"||DX5||"'"),compress("'"||DX6||"'"), compress("'"||DX7||"'"), compress("'"||DX8||"'"), compress("'"||DX9||"'"), compress("'"||DX10||"'")) 
    into :DX 
    from diseases where disease="&amp;amp;&amp;amp;DIS&amp;amp;i";
quit;

%put &amp;amp;DX;

   
 data &amp;amp;&amp;amp;DIS&amp;amp;i (Keep= subject &amp;amp;&amp;amp;DIS&amp;amp;i);
 set work.fake_patients;
array icd_codes[10] $ ICD1-ICD10;

&amp;amp;&amp;amp;DIS&amp;amp;i=0;
do j = 1 to dim(icd_codes);
        if icd_codes[j] in  (&amp;amp;DX) then do;
&amp;amp;&amp;amp;DIS&amp;amp;i=1;
leave;
 
        end;
    end;
	
run;


		
%end;

%mend;

%sendR2D2(data=diseases,var=disease);


Data want2;
merge work.fake_patients work.asthma work.diabetes work.hypertension work.pneumonia;
by subject;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What doesn't work and need to resolve:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data fake_patients;
    length subject $5 ICD1-ICD10 $7;
    array icd_codes[10] $ ICD1-ICD10;

    /* List of ICD-10 codes for various diseases */
    array pneumonia_codes[10] $7 _temporary_ ('J12.0', 'J12.1', 'J12.2', 'J12.3', 'J12.4', 
                                              'J12.5', 'J12.8', 'J12.9', 'J13', 'J14');
    array diabetes_codes[8] $7 _temporary_ ('E10.9', 'E11.9', 'E13.9', 'E14.9', 
                                            'E10.65', 'E11.65', 'E13.65', 'E14.65');
    array hypertension_codes[10] $7 _temporary_ ('I10', 'I11.0', 'I11.9', 'I12.0', 
                                                 'I12.9', 'I13.0', 'I13.10', 'I13.11', 
                                                 'I13.2', 'I15.0');
    array asthma_codes[10] $7 _temporary_ ('J45.20', 'J45.21', 'J45.22', 'J45.30', 
                                           'J45.31', 'J45.32', 'J45.40', 'J45.41', 
                                           'J45.42', 'J45.50');
    array other_codes[10] $7 _temporary_ ('A00', 'A01', 'A02', 'A03', 'A04', 
                                          'A05', 'A06', 'A07', 'A08', 'A09');
    
    /* Create 50 fake patients */
    do subject_id = 1 to 50;
        subject = cats('S', put(subject_id, z3.));

        /* Assign random ICD-10 codes to each patient */
        do i = 1 to 10;
            /* Randomly choose a disease category */
            disease_category = rand('integer', 1, 5);
            
            select (disease_category);
                when (1) icd_codes[i] = pneumonia_codes[rand('integer', 1, dim(pneumonia_codes))];
                when (2) icd_codes[i] = diabetes_codes[rand('integer', 1, dim(diabetes_codes))];
                when (3) icd_codes[i] = hypertension_codes[rand('integer', 1, dim(hypertension_codes))];
                when (4) icd_codes[i] = asthma_codes[rand('integer', 1, dim(asthma_codes))];
                when (5) icd_codes[i] = other_codes[rand('integer', 1, dim(other_codes))];
            end;
        end;

        output;
    end;
run;




data diseases;
    length disease $20 DX1-DX10 $7;

    /* Pneumonia and its ICD-10 codes */
    disease = "Pneumonia";
    DX1 = 'J12.0'; DX2 = 'J12.1'; DX3 = 'J12.2'; DX4 = 'J12.3';
    DX5 = 'J12.4'; DX6 = 'J12.5'; DX7 = 'J12.8'; DX8 = 'J12.9';
    DX9 = 'J13'; DX10 = 'J14';
    output;

    /* Diabetes and its ICD-10 codes */
    disease = "Diabetes";
    DX1 = 'E10.9'; DX2 = 'E11.9'; DX3 = 'E13.9'; DX4 = 'E14.9';
    DX5 = 'E10.65'; DX6 = 'E11.65'; DX7 = 'E13.65'; DX8 = 'E14.65';
    DX9 = ''; DX10 = '';
    output;

    /* Hypertension and its ICD-10 codes */
    disease = "Hypertension";
    DX1 = 'I10'; DX2 = 'I11.0'; DX3 = 'I11.9'; DX4 = 'I12.0';
    DX5 = 'I12.9'; DX6 = 'I13.0'; DX7 = 'I13.10'; DX8 = 'I13.11';
    DX9 = 'I13.2'; DX10 = 'I15.0';
    output;

    /* Asthma and its ICD-10 codes */
    disease = "Asthma";
    DX1 = 'J45.20'; DX2 = 'J45.21'; DX3 = 'J45.22'; DX4 = 'J45.30';
    DX5 = 'J45.31'; DX6 = 'J45.32'; DX7 = 'J45.40'; DX8 = 'J45.41';
    DX9 = 'J45.42'; DX10 = 'J45.50';
    output;
run;

%macro sendR2D3 (data=, var=);
   proc sort data=&amp;amp;data(keep=&amp;amp;var) out=values nodupkey;
       by &amp;amp;var;
   run;

   data _null_;
      set values end=last;
      call symputx('DIS'||left(_n_), &amp;amp;var);
      if last then call symputx('countx', _n_, 'g');
   run;

%put _local_;

%do i=1 %to &amp;amp;countx;

proc sql noprint;
    select  catx(',', compress("'"||DX1||"'"), compress("'"||DX2||"'"), compress("'"||DX3||"'"), 
compress("'"||DX4||"'"),compress("'"||DX5||"'"),compress("'"||DX6||"'"), compress("'"||DX7||"'"), compress("'"||DX8||"'"), compress("'"||DX9||"'"), compress("'"||DX10||"'")) 
    into :DX 
    from diseases where disease="&amp;amp;&amp;amp;DIS&amp;amp;i";
quit;

%put &amp;amp;DX;

   
array icd_codes[10] $ ICD1-ICD10;

&amp;amp;&amp;amp;DIS&amp;amp;i=0;
do j = 1 to dim(icd_codes);
        if icd_codes[j] in  (&amp;amp;DX) then do;
&amp;amp;&amp;amp;DIS&amp;amp;i=1;
leave;
 
        end;
    end;
	
run;


		
%end;

%mend;





Data want3;
set work.fake_patients;
%sendR2D3(data=diseases,var=disease);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Aug 2024 20:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938700#M368678</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-08T20:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938736#M368681</link>
      <description>&lt;P&gt;If a macro contains data step code or procedure then when the DATA or Proc statement is encountered it ends the data step.&lt;/P&gt;
&lt;P&gt;What would expect to have happen with:&lt;/P&gt;
&lt;PRE&gt;data junk; 
   set sashelp.class;
   ratio = weight/height;
proc sort data=sashelp.class
   out=dummy;
  by sex;
run;&lt;/PRE&gt;
&lt;P&gt;If your answer is not something like "when the data junk step encounters the Proc sort statement then an implied run is used and the data set junk is created with the added variable using the sashelp.class data. Then the sort executes."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other statements may have interesting interactions depending on exactly which class of statement is generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also have a timing issue about when MACRO variables are resolved in data step code. They are resolved at compilation time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm too tired at the moment to spend anytime trying to determine what your "need to resolve" code is attempting to do, especially since you didn't even provide a brief description of intent.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 22:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938736#M368681</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-08T22:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938742#M368685</link>
      <description>&lt;P&gt;Agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'s analysis.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Part of the hard work of asking an answerable question is to build a *small* clear example that illustrates the problem.&amp;nbsp; Typically this can be done with a sample dataset of 5-10 records, and just one or two steps.&amp;nbsp; But doing that requires you to simplify your problem dramatically, until you have a minimal reproducible example (MRE).&amp;nbsp; Many times as you try to construct an MRE, you will find the answer.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you construct an MRE and cannot find the answer, please post it.&amp;nbsp; And also, in the context of the MRE, please don't say that it 'doesn't work'.&amp;nbsp; Instead, tell us whether you have errors/warnings in the log, or if the log is clean but you are getting surprising results.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2024 01:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938742#M368685</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-08-09T01:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938791#M368700</link>
      <description>&lt;P&gt;If this is supposed to be some sort of look up to tell if a specific instance of DX is a specific disease related ICD code then perhaps a custom format would be the way to look things up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format 
value code2disease
'J12.0', 'J12.1', 'J12.2', 'J12.3', 'J12.4',
'J12.5', 'J12.8', 'J12.9', 'J13'  , 'J14'      = "Pneumonia"
/* repeat for others of interest*/
other='Not listed'
;

run;&lt;/PRE&gt;
&lt;P&gt;And use in a data step might look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if put(dx[i], code2disease.) ='Pneumonia' then &amp;lt;do what you want when found&amp;gt; &lt;/PRE&gt;
&lt;P&gt;Or a similar informat.&lt;/P&gt;
&lt;P&gt;One thing about the format/ informat approach would be that you can have varying numbers of codes you don't have to try to keep track, the SAS use of the format maintains the list size and uses it automaticaly.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2024 10:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938791#M368700</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-09T10:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938800#M368704</link>
      <description>&lt;P&gt;When running macros make sure that the MPRINT option is turned on so you can see in the SAS log the SAS code that the macro generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This part of you macro makes no sense:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to &amp;amp;countx;
proc sql noprint;
    select  catx(',', compress("'"||DX1||"'"), compress("'"||DX2||"'"), compress("'"||DX3||"'"), 
compress("'"||DX4||"'"),compress("'"||DX5||"'"),compress("'"||DX6||"'"), compress("'"||DX7||"'"), compress("'"||DX8||"'"), compress("'"||DX9||"'"), compress("'"||DX10||"'")) 
    into :DX 
    from diseases where disease="&amp;amp;&amp;amp;DIS&amp;amp;i";
quit;
%put &amp;amp;DX;
array icd_codes[10] $ ICD1-ICD10;
&amp;amp;&amp;amp;DIS&amp;amp;i=0;
do j = 1 to dim(icd_codes);
    if icd_codes[j] in  (&amp;amp;DX) then do;
        &amp;amp;&amp;amp;DIS&amp;amp;i=1;
        leave;
    end;
 end;
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When the macro variable I has the value of 1 you generate a PROC SQL step.&amp;nbsp; Then immediately generate an ARRAY statement.&amp;nbsp; You cannot have an ARRAY statement outside of a data step.&amp;nbsp; Even if you remove the PROC SQL code it does not make any sense to the have the ARRAY statement inside the %DO loop.&amp;nbsp; You only want to have one ARRAY statement for the array named ICD_CODES.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2024 13:28:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938800#M368704</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-09T13:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938986#M368756</link>
      <description>&lt;P&gt;I do apologize that description was way too terse. In short in the 2nd dataset -diseases -this will be a dynamic dataset that will be updated probably monthly or quarterly with at least 50 disease categories.&amp;nbsp; While the fake_patients dataset has 50 patients and 10 ICD codes in reality - will be getting all the diagnosis codes in a horizontal fashion&amp;nbsp; with 200,000 actual patients.&amp;nbsp; The first example already works and if I have to I will use that,&amp;nbsp; However if I can get the 2nd example to work it would be more efficient.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 16:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938986#M368756</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T16:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938987#M368757</link>
      <description>&lt;P&gt;Interesting idea. I could output a dynamic&amp;nbsp; script with new ICD codes. I'll have to think about that.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 16:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938987#M368757</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T16:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938990#M368758</link>
      <description>&lt;P&gt;Thank you for your response. But then if I write something to this effect:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sendR2D3(data=, var=);
   proc sort data=&amp;amp;data(keep=&amp;amp;var) out=values nodupkey;
       by &amp;amp;var;
   run;

   data _null_;
      set values end=last;
      call symputx('DIS'||left(_n_), &amp;amp;var);
      if last then call symputx('countx', _n_, 'g');
   run;

%put _local_;

%do i=1 %to &amp;amp;countx;

&amp;amp;&amp;amp;DIS&amp;amp;i=0;

		
%end;

%mend;




Data want3;
set work.fake_patients;
%sendR2D3(data=diseases,var=disease);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and I run it I get a 'Statement is not valid or it is used out of proper order.'&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can see that it does&amp;nbsp; generate the macro variable though -EX NOTE: Line generated by the macro variable "DIS1".&lt;BR /&gt;133 Asthma&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I just run:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want3;
set work.fake_patients;
asthma=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is fine. SO it is my lack of understanding&amp;nbsp; of what I can/can't do with macro in respects to running it within a data step.&amp;nbsp; Thank you,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 16:54:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938990#M368758</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T16:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938992#M368760</link>
      <description>&lt;P&gt;If you are having trouble visualizing what CODE the macro will generate use the MPRINT option and you SEE it in the LOG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That macro will first run a PROC step, then a DATA step.&amp;nbsp; Then it will EMIT the values of a series of macro variables.&amp;nbsp; So if the values of variable named DISEASE in the dataset named DISEASES has valid SAS code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diseases;
  disease='data x; set y; run;' ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your macro makes some sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if instead DISEASE just have values like: A21, B54 then it makes no sense to have the macro generate code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort; .....
data ....
A21;
B54;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SAS language complier is not going to understand those last two statements.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 17:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938992#M368760</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-12T17:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938997#M368765</link>
      <description>&lt;P&gt;Ok-Thank you for that info. I do have the MPRINT option&amp;nbsp; on-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I attempt to execute&amp;nbsp; the macro&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would go like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data set3;&lt;/P&gt;
&lt;P&gt;set fake_patients;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;---macro statement here---&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;macro renders as asthma=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so to me -it appears that the macro var is defined within the proc step. But obviously not!&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 17:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/938997#M368765</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T17:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939003#M368768</link>
      <description>&lt;P&gt;Let's take your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
  set work.fake_patients;
%sendR2D3(data=diseases,var=disease);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While SAS is compiling that code it first sees the DATA statement. So it knows you want to start a new step.&amp;nbsp; So if you had previously had something like a PROC or DATA step without an terminal command like RUN or QUIT ( or DATALINES for a data step) then that previous steps compilation is finished and the step runs.&amp;nbsp; Now it starts trying to build the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it sees the SET statement.&amp;nbsp; So it knows to add the variables from FAKE_PATIENTS into the data vector for this data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the macro pre-processor sees the macro call.&amp;nbsp; So it takes over and begins running the macro.&amp;nbsp; Sending any statements it emits back to the SAS compiler to evaluate.&amp;nbsp; So the first statement that the macro emits is a PROC SORT statement.&amp;nbsp; So the SAS compiler sees that and knows that the definition of the DATA step is complete. So the data step runs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next the macro emits the rest of the PROC SORT step. So that now runs.&amp;nbsp; Then it emits a DATA step, so that runs.&amp;nbsp; Then it emits the values of the macro variables.&amp;nbsp; Which are not valid outside of a DATA step so the SAS compiler generates an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, after the macro has finished, the SAS compiler sees the RUN statement in the program source.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 17:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939003#M368768</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-12T17:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939004#M368769</link>
      <description>&lt;P&gt;Got it. Makes sense now. Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 17:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939004#M368769</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T17:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939007#M368770</link>
      <description>&lt;P&gt;Here is an order of operations that makes sense for your example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First process the dataset and generate the assignment statements into a FILE. Then use %INCLUDE to execute the statements in the FILE where you want them to appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your macro might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sendR2D3(data=, var=, file=);
proc sort data=&amp;amp;data(keep=&amp;amp;var) out=values nodupkey;
  by &amp;amp;var;
run;

data _null_;
  set values ;
  file &amp;amp;file;
  put &amp;amp;var '=0;' ;
run;
%mend ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you could then use it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
%sendR2D3(data=diseases,var=disease,file=code);
data want3;
  set work.fake_patients;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Aug 2024 18:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939007#M368770</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-12T18:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: embedding macro in data step  (involving array)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939019#M368773</link>
      <description>This is very helpful in understanding how  it all works. Thank you.</description>
      <pubDate>Mon, 12 Aug 2024 20:32:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/embedding-macro-in-data-step-involving-array/m-p/939019#M368773</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2024-08-12T20:32:42Z</dc:date>
    </item>
  </channel>
</rss>

