<?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 Add blank columns to data set from macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806298#M317644</link>
    <description>&lt;P&gt;I have a multiple datasets with many columns. I am attempting to go through each one and order the columns and add in any columns stored in a macro variable that aren't in the current table. See below for example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input var7 4. var3 4. var2 4. var5 4. var1 4.;&lt;BR /&gt;datalines;&lt;BR /&gt;492 234 230 921 394&lt;BR /&gt;843 230 102 748 239&lt;BR /&gt;;&lt;/PRE&gt;&lt;P&gt;Here is what I want.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt; data want;
 input var1 4. var2 4. var3 4. var4 4. var5 4. var6 4. var7 4.;
 datalines;
394 230 234     921     492
239 102 230     748     843
 ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have the names of the columns stored in a macro variable that is structured like below.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
    select name
    into :my_column_order separated by " "
    from DICTIONARY.COLUMNS
    where UPCASE(LIBNAME) = "WORK"
    and UPCASE(MEMNAME) = "WANT"
    order by name;
quit;

%put &amp;amp;my_column_order;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a way to use my have dataset and &amp;amp;my_column_order to add in and correctly order missing columns and format them the same as the other columns?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Apr 2022 14:28:36 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2022-04-06T14:28:36Z</dc:date>
    <item>
      <title>Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806298#M317644</link>
      <description>&lt;P&gt;I have a multiple datasets with many columns. I am attempting to go through each one and order the columns and add in any columns stored in a macro variable that aren't in the current table. See below for example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input var7 4. var3 4. var2 4. var5 4. var1 4.;&lt;BR /&gt;datalines;&lt;BR /&gt;492 234 230 921 394&lt;BR /&gt;843 230 102 748 239&lt;BR /&gt;;&lt;/PRE&gt;&lt;P&gt;Here is what I want.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt; data want;
 input var1 4. var2 4. var3 4. var4 4. var5 4. var6 4. var7 4.;
 datalines;
394 230 234     921     492
239 102 230     748     843
 ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have the names of the columns stored in a macro variable that is structured like below.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
    select name
    into :my_column_order separated by " "
    from DICTIONARY.COLUMNS
    where UPCASE(LIBNAME) = "WORK"
    and UPCASE(MEMNAME) = "WANT"
    order by name;
quit;

%put &amp;amp;my_column_order;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a way to use my have dataset and &amp;amp;my_column_order to add in and correctly order missing columns and format them the same as the other columns?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 14:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806298#M317644</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2022-04-06T14:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806306#M317648</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var7 4. var3 4. var2 4. var5 4. var1 4.;
datalines;
492 234 230 921 394
843 230 102 748 239
;

 data want;
 input var1 4. var2 4. var3 4. var4 4. var5 4. var6 4. var7 4.;
 datalines;
394 230 234     921     492
239 102 230     748     843
 ;

proc sql;
    select name
    into :my_column_order separated by " "
    from DICTIONARY.COLUMNS
    where UPCASE(LIBNAME) = "WORK"
    and UPCASE(MEMNAME) = "WANT"
    order by name;
quit;

%put &amp;amp;my_column_order;

data want_final;
	retain &amp;amp;my_column_order.;
	set have;
	array vars [*] 4. &amp;amp;my_column_order.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="maguiremq_0-1649255911236.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70169iE57726F329F507D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="maguiremq_0-1649255911236.png" alt="maguiremq_0-1649255911236.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 14:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806306#M317648</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-04-06T14:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806311#M317652</link>
      <description>&lt;P&gt;Your example implies that the variables are all numeric. Is this in fact the case? If not you may have quite a bit more work, especially if any of the variables are of different data types in different data sets with the same name. Since you did not pull the FORMAT information from that Have data set, I don't think so.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why? If you intend to combine the data then in a data step or Proc append the order of the variables in the first data set listed will be the resulting order and sets the format and length of the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 14:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806311#M317652</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-06T14:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806313#M317654</link>
      <description>&lt;P&gt;Is a macro variable really needed here? I don't think so:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var7 4. var3 4. var2 4. var5 4. var1 4.;
datalines;
492 234 230 921 394
843 230 102 748 239
;

data have0;
    input var1-var7;
run;

data want;
    set have0 have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 14:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806313#M317654</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-06T14:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806317#M317656</link>
      <description>&lt;P&gt;Or even:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var7 4. var3 4. var2 4. var5 4. var1 4.;
datalines;
492 234 230 921 394
843 230 102 748 239
;

data want;
    array v var1-var7;
    set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 15:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806317#M317656</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-04-06T15:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806319#M317657</link>
      <description>&lt;P&gt;Can be done in one easy, simple step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_final;
set
  want (obs=0)
  have
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Apr 2022 15:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806319#M317657</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-06T15:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806326#M317661</link>
      <description>&lt;P&gt;If your task is to re-order variables already in a SAS dataset, then you have lots of working solutions available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you are really starting out with raw data that you wish to re-order, then you don't need an extra DATA step, just a minor change in the first DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length var1-var7 8;
  input var7 4. var3 4. var2 4. var5 4. var1 4.  ... other vars ... ;
datalines;
....
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 15:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806326#M317661</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-06T15:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806331#M317665</link>
      <description>&lt;P&gt;I don't have a data set with a list of all the variables. The macro variable pulling from want was to demonstrate what the variable looks like. I.e., I don't have my want data set and I can't list all my variables out.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 16:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806331#M317665</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2022-04-06T16:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806332#M317666</link>
      <description>I don't have the "want" dataset.</description>
      <pubDate>Wed, 06 Apr 2022 16:02:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806332#M317666</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2022-04-06T16:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806334#M317667</link>
      <description>&lt;P&gt;Also simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
retain &amp;amp;my_column.;
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Apr 2022 16:05:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806334#M317667</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-06T16:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806337#M317669</link>
      <description>this does not produce the output "want" for me.</description>
      <pubDate>Wed, 06 Apr 2022 16:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806337#M317669</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2022-04-06T16:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806338#M317670</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/234586"&gt;@A_SAS_Man&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't have a data set with a list of all the variables. The macro variable pulling from want was to demonstrate what the variable looks like. I.e., I don't have my want data set and I can't list all my variables out.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Providing code/information that is not actually available, such as your Proc SQL obfuscates the entire problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You specifically included Formats in your request. So, where do you create this macro variable? And where do you want to assign formats?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you say that you have "multiple data set" perhaps modify that code to use: "and member in ("FIRSTSET" "SECONDSET" "OTHERDATASET") , listing all your actual data set names and Select distinct name. Then you would have list of all variables in all the data sets to play with.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 16:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806338#M317670</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-06T16:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806341#M317673</link>
      <description>&lt;P&gt;We need to use the array method to create the additional variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array x{*} &amp;amp;my_column_order.;
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this will make all variables numeric. If you also have character variables, this will fail.&lt;/P&gt;
&lt;P&gt;So we need to know more about your&amp;nbsp;&lt;EM&gt;real&lt;/EM&gt; issue, and the &lt;EM&gt;real&lt;/EM&gt; resources you have available.&lt;/P&gt;
&lt;P&gt;Post the code that creates the macro variable, and/or post the macro variable itself in a code box (&amp;lt;/&amp;gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 16:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806341#M317673</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-06T16:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Add blank columns to data set from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806344#M317675</link>
      <description>&lt;P&gt;If you have a macro variable with the space delimited names that must exist then use that to generate code that will insure those variable names exist.&lt;/P&gt;
&lt;P&gt;For example you might have one of these values for MY_ORDER_VARIABLE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let my_order_variable=v1 v2 v3 v3 v4 v5 v6 v7;
%let my_order_variable=v1-v7;
%let my_order_variable=v7 v3 v6-v4 v1 v2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that you could use it in a data step like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  retain &amp;amp;my_order_variable;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if you need some of those variables to be of type CHAR instead of type NUM then you need more information than just the names of the variables.&amp;nbsp; Specifically you need to know the LENGTH.&amp;nbsp; So if for example V6 should be a character variable that can hold up to 20 bytes and the other 6 variables are numeric you might make the macro variable hold something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let my_order_variable=v1 v2 v3 v3 v4 v5 8 v6 $20 v7 8;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you could then use with a LENGTH statement to define the variables and their order.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length &amp;amp;my_order_variable;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if any of the variables require a special format to be attached to them, such as DATE or DATETIME values, then you need even more information than just the variable's type and storage length.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 16:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-blank-columns-to-data-set-from-macro-variable/m-p/806344#M317675</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-06T16:40:42Z</dc:date>
    </item>
  </channel>
</rss>

