<?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: Variables Using Count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241149#M44648</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This response is pretty similar to dcruik's response (I think we both started responding around the same time).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, I added some sample data. Also, placing the comma before the derivation_1, derivation_2, etc. statement is handy in that it avoids the need to&amp;nbsp;add an&amp;nbsp;"&lt;EM&gt;%else&amp;nbsp;%do&lt;/EM&gt;" statement.&amp;nbsp;Enjoy.&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;&lt;CODE class=" language-sas"&gt;%let num = 4; *fictitious value;
%let standard_vars = %STR(abc, xyz); *fictitious variables;


/* fictitious dataset to mimic yours */
data edd_derivations;
  do i=1 to 200;
    event_type = round(ranuni(123)*25);
    output;
  end;
run;


proc sql;
  select count(*) into :var_count 
  from edd_derivations 
  where event_type=&amp;amp;num.;
quit;
%put &amp;amp;var_count;


/* fictitious dataset to mimic yours */
data _events_;
  length event_type abc xyz $8;
  do j=1 to 500;
    event_type = cats(round(ranuni(456)*25), "");
    abc = byte(65 + mod(j, 26));&lt;BR /&gt;    xyz = byte(90 - mod(j, 26));
    output;
  end;
run;


/* this version, "DynamicSQL1()", shows the general approach */
%MACRO DynamicSQL1();

  proc sql;
    create table _&amp;amp;num. as 
    select &amp;amp;standard_vars.
    %IF %EVAL(&amp;amp;var_count &amp;gt; 0) %THEN %DO;
      %DO k=1 %TO &amp;amp;var_count;
         , CASE WHEN /* some condition goes here [e.g. " abc in ('A','B','C')" ] */ THEN ':-)' ELSE ':( ' END AS name_&amp;amp;k.
      %END;
    %END;
    from _events_
    where event_type="&amp;amp;num."
    ;
  quit;

%MEND;
%DynamicSQL1();


/* this version, "DynamicSQL2()", is intended to mimic your example; 
   I wasn't sure if your derivation_1, derivation_2, etc. variables 
   were dynamic or stayed constant throughout */
%MACRO DynamicSQL2();

  /* these macro variables are silly placeholders; 
     please replace with your own logic */
  %let derivation_1 = %str(CASE WHEN 1=2 THEN 'sad' ELSE 'happy' END);
  %let derivation_2 = %str(CASE WHEN 2=3 THEN 'sad' ELSE 'happy' END);
  %let derivation_3 = %str(CASE WHEN 3=4 THEN 'sad' ELSE 'happy' END);
  %let derivation_4 = %str(CASE WHEN 4=5 THEN 'sad' ELSE 'happy' END);
  %let derivation_5 = %str(CASE WHEN 5=6 THEN 'sad' ELSE 'happy' END);

  proc sql;
    create table _&amp;amp;num. as 
    select &amp;amp;standard_vars.
    %IF %EVAL(&amp;amp;var_count &amp;gt; 0) %THEN %DO;
      %DO k=1 %TO &amp;amp;var_count;
         /* note the double ampersand before the derivation variable ... */
         , &amp;amp;&amp;amp;derivation_&amp;amp;k. AS name_&amp;amp;k.
      %END;
    %END;
    from _events_
    where event_type="&amp;amp;num."
    ;
  quit;

%MEND;
%DynamicSQL2();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Dec 2015 19:24:51 GMT</pubDate>
    <dc:creator>hbi</dc:creator>
    <dc:date>2015-12-29T19:24:51Z</dc:date>
    <item>
      <title>Variables Using Count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241133#M44645</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking to create a number of different tables which are broadly similar but have a different number of additional variable which are calculated. So standard_vars will always be standard but the count of other variables I'd like determined on the select (*) from the first statement. I.e. if count(*) is 2 then I'd only have 2 additional variables in the create table statement. Would anybody be able to help me limit the variables in the second statement? Any help would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql; select count(*) into :var_count from edd_derivations where event_type=&amp;amp;num.;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql; create table _&amp;amp;num. as select &lt;BR /&gt;&amp;amp;standard_vars.,&lt;BR /&gt;&amp;amp;derivation_1. as &amp;amp;name_1.,&lt;BR /&gt;&amp;amp;derivation_2. as &amp;amp;name_2.,&lt;BR /&gt;&amp;amp;derivation_3. as &amp;amp;name_3.,&lt;BR /&gt;&amp;amp;derivation_4. as &amp;amp;name_4.&lt;BR /&gt;from _events_&lt;BR /&gt;and event_type="&amp;amp;num."&lt;BR /&gt;;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2015 14:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241133#M44645</guid>
      <dc:creator>scott_darge</dc:creator>
      <dc:date>2015-12-29T14:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Variables Using Count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241148#M44647</link>
      <description>&lt;P&gt;Just to help me understand a little more, in your SQL procedure, the &amp;amp;derivation_1. as &amp;amp;name_1. through &amp;amp;derivation_4. as &amp;amp;name_4. are the additional variables you want?&amp;nbsp; So, in the case you presented, the var_count macro variable would resolve to 4?&amp;nbsp; If that is the case, then I believe the below code would create as many columns called "&amp;amp;name_&lt;EM&gt;n&lt;/EM&gt;" as the value of the var_count macro (in the case you presented, it would list 4 fields).&amp;nbsp; Also, this is obviously assuming that you have values for the macro variables num, derivation, and name being called.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps, if it's not what you're looking for, maybe give a better example of the data you have and what you'd like to see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, just an observation, but you have the event type as a character format in the _events_ data set and a numeric format in the edd_derivations data set.&amp;nbsp; Was that intended, or do you need to add/remove "" from one of the SQL procedures?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select count(*) into: var_count
from edd_derivations
where event_type=&amp;amp;num;
quit;

%macro create;
	proc sql;
	create table _&amp;amp;num as
		select &amp;amp;standard_vars,
			   %do i=1 %to &amp;amp;var_count;
			   		%if &amp;amp;i=&amp;amp;var_count %then %do;
						&amp;amp;derivation_&amp;amp;i as &amp;amp;name_&amp;amp;i
					%end;
					%else %do;
						&amp;amp;derivation_&amp;amp;i as &amp;amp;name_&amp;amp;i,
					%end;
				%end;
	from _events_
	where event_type="&amp;amp;num";
	quit;
%mend;

%create;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2015 19:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241148#M44647</guid>
      <dc:creator>dcruik</dc:creator>
      <dc:date>2015-12-29T19:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Variables Using Count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241149#M44648</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This response is pretty similar to dcruik's response (I think we both started responding around the same time).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, I added some sample data. Also, placing the comma before the derivation_1, derivation_2, etc. statement is handy in that it avoids the need to&amp;nbsp;add an&amp;nbsp;"&lt;EM&gt;%else&amp;nbsp;%do&lt;/EM&gt;" statement.&amp;nbsp;Enjoy.&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;&lt;CODE class=" language-sas"&gt;%let num = 4; *fictitious value;
%let standard_vars = %STR(abc, xyz); *fictitious variables;


/* fictitious dataset to mimic yours */
data edd_derivations;
  do i=1 to 200;
    event_type = round(ranuni(123)*25);
    output;
  end;
run;


proc sql;
  select count(*) into :var_count 
  from edd_derivations 
  where event_type=&amp;amp;num.;
quit;
%put &amp;amp;var_count;


/* fictitious dataset to mimic yours */
data _events_;
  length event_type abc xyz $8;
  do j=1 to 500;
    event_type = cats(round(ranuni(456)*25), "");
    abc = byte(65 + mod(j, 26));&lt;BR /&gt;    xyz = byte(90 - mod(j, 26));
    output;
  end;
run;


/* this version, "DynamicSQL1()", shows the general approach */
%MACRO DynamicSQL1();

  proc sql;
    create table _&amp;amp;num. as 
    select &amp;amp;standard_vars.
    %IF %EVAL(&amp;amp;var_count &amp;gt; 0) %THEN %DO;
      %DO k=1 %TO &amp;amp;var_count;
         , CASE WHEN /* some condition goes here [e.g. " abc in ('A','B','C')" ] */ THEN ':-)' ELSE ':( ' END AS name_&amp;amp;k.
      %END;
    %END;
    from _events_
    where event_type="&amp;amp;num."
    ;
  quit;

%MEND;
%DynamicSQL1();


/* this version, "DynamicSQL2()", is intended to mimic your example; 
   I wasn't sure if your derivation_1, derivation_2, etc. variables 
   were dynamic or stayed constant throughout */
%MACRO DynamicSQL2();

  /* these macro variables are silly placeholders; 
     please replace with your own logic */
  %let derivation_1 = %str(CASE WHEN 1=2 THEN 'sad' ELSE 'happy' END);
  %let derivation_2 = %str(CASE WHEN 2=3 THEN 'sad' ELSE 'happy' END);
  %let derivation_3 = %str(CASE WHEN 3=4 THEN 'sad' ELSE 'happy' END);
  %let derivation_4 = %str(CASE WHEN 4=5 THEN 'sad' ELSE 'happy' END);
  %let derivation_5 = %str(CASE WHEN 5=6 THEN 'sad' ELSE 'happy' END);

  proc sql;
    create table _&amp;amp;num. as 
    select &amp;amp;standard_vars.
    %IF %EVAL(&amp;amp;var_count &amp;gt; 0) %THEN %DO;
      %DO k=1 %TO &amp;amp;var_count;
         /* note the double ampersand before the derivation variable ... */
         , &amp;amp;&amp;amp;derivation_&amp;amp;k. AS name_&amp;amp;k.
      %END;
    %END;
    from _events_
    where event_type="&amp;amp;num."
    ;
  quit;

%MEND;
%DynamicSQL2();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2015 19:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241149#M44648</guid>
      <dc:creator>hbi</dc:creator>
      <dc:date>2015-12-29T19:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Variables Using Count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241152#M44649</link>
      <description>Where do &amp;amp;derivation_1 and &amp;amp;name_1 get declared/assigned? Is there a naming convention to the variables? I wonder if a data step would be more efficient if there was a naming convention. For example if the variables are listed in the dd_derivations list that could be queried as well.&lt;BR /&gt;&lt;BR /&gt;data table_&amp;amp;num;&lt;BR /&gt;set _events_;&lt;BR /&gt;&lt;BR /&gt;keep &amp;amp;standard_vars &amp;amp;derivation_1-&amp;amp;derivation_&amp;amp;var_count;&lt;BR /&gt;rename &amp;amp;derivation_1-&amp;amp;derivation_&amp;amp;var_count=&amp;amp;name1-&amp;amp;&amp;amp;name_&amp;amp;var_count.;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 29 Dec 2015 20:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variables-Using-Count/m-p/241152#M44649</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-29T20:02:55Z</dc:date>
    </item>
  </channel>
</rss>

