<?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 Concatenate characteristic and numeric variables for multiple files and numerous variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476725#M286126</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately I have a rather non user friendly data structure. It consists of 200 txt files (dlm = ',') that have to be &lt;SPAN&gt;concatenated. Each files has +/- 2 observations and 453 variables. I have written some code to read the 200 data files like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%symdel start eind; 
%let start=1;
%let eind=9;
%macro loop_attr_2017_10;
%let i=&amp;amp;start;
%do %while (&amp;amp;i &amp;lt;= &amp;amp;eind );
proc import datafile="$FSROOT/.../attributes.long.55769e3d9a318c4312000009.csv/part-0000&amp;amp;i."
     out=attr_2017_&amp;amp;i.
     dbms=dlm
     replace;&lt;BR /&gt;     DELIMITER = ',';
     getnames=yes;
     GUESSINGROWS=2147483647;
run;
%let i=%eval(&amp;amp;i+1);
%end;
%mend;
%loop_attr_2017_10;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;After I have read the data, I want to concatenate the 200 files (attr_2017_0 -&amp;gt; attr_2017_199) to one file (attributes_2017).&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data attributes_2017;
	set ATTR_2017_0;
run;
%let conBegin=1;
%let conEind=199;
%macro concet_attr_2017;
%let i = &amp;amp;conBegin;
%do %while (&amp;amp;i &amp;lt;= &amp;amp;conEind);
data attributes_2017;
	set attributes_2017 attr_2017_&amp;amp;i.;
run;
%let i=%eval(&amp;amp;i+1);
%end;
%mend;
%concet_attr_2017;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But this&amp;nbsp;is where the troubles arise. For some files , some values of the 453 variables are missing for all the subjects in that file. So SAS will read that variable by default as a &lt;STRONG&gt;character&lt;/STRONG&gt;. But, when in the next file the value is not missing and it is a &lt;STRONG&gt;numeric&lt;/STRONG&gt; variable, it becomes impossible to concatenate the two files.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Variable activity_first_time_value has been defined as both character and numeric.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;For example 'activity_first_time_value' in the file 'ATTR_2017_3' is &lt;STRONG&gt;characteristic&lt;/STRONG&gt;, while in 'ATTR_2017_4' it is &lt;STRONG&gt;numeric&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="c1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21684i9CE6ACB57196246E/image-size/large?v=v2&amp;amp;px=999" role="button" title="c1.PNG" alt="c1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="c2.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21685iE3C7CA9DEB44E5F4/image-size/large?v=v2&amp;amp;px=999" role="button" title="c2.PNG" alt="c2.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Since I have 453 variables and 200 files to concatenate, it is too much work to account for it individually. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;U&gt;So my question is; is there a way the program this concatenate is such a way that whenever SAS &amp;nbsp;detects in the &amp;nbsp;next file the variables as numeric, the variable in the previous file is also changed into numeric? Or another work around?&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;Kind Regards,&lt;/P&gt;&lt;P&gt;Leonard&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: I also consulted &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/can-we-concatenate-a-numeric-and-character-and-make-it-to/td-p/359905" target="_self"&gt;this&lt;/A&gt;&amp;nbsp;page, but as mentioned before, I believe there a too much variables to account for it manually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 Jul 2018 10:20:18 GMT</pubDate>
    <dc:creator>Leonard32</dc:creator>
    <dc:date>2018-07-10T10:20:18Z</dc:date>
    <item>
      <title>Concatenate characteristic and numeric variables for multiple files and numerous variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476725#M286126</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately I have a rather non user friendly data structure. It consists of 200 txt files (dlm = ',') that have to be &lt;SPAN&gt;concatenated. Each files has +/- 2 observations and 453 variables. I have written some code to read the 200 data files like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%symdel start eind; 
%let start=1;
%let eind=9;
%macro loop_attr_2017_10;
%let i=&amp;amp;start;
%do %while (&amp;amp;i &amp;lt;= &amp;amp;eind );
proc import datafile="$FSROOT/.../attributes.long.55769e3d9a318c4312000009.csv/part-0000&amp;amp;i."
     out=attr_2017_&amp;amp;i.
     dbms=dlm
     replace;&lt;BR /&gt;     DELIMITER = ',';
     getnames=yes;
     GUESSINGROWS=2147483647;
run;
%let i=%eval(&amp;amp;i+1);
%end;
%mend;
%loop_attr_2017_10;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;After I have read the data, I want to concatenate the 200 files (attr_2017_0 -&amp;gt; attr_2017_199) to one file (attributes_2017).&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data attributes_2017;
	set ATTR_2017_0;
run;
%let conBegin=1;
%let conEind=199;
%macro concet_attr_2017;
%let i = &amp;amp;conBegin;
%do %while (&amp;amp;i &amp;lt;= &amp;amp;conEind);
data attributes_2017;
	set attributes_2017 attr_2017_&amp;amp;i.;
run;
%let i=%eval(&amp;amp;i+1);
%end;
%mend;
%concet_attr_2017;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But this&amp;nbsp;is where the troubles arise. For some files , some values of the 453 variables are missing for all the subjects in that file. So SAS will read that variable by default as a &lt;STRONG&gt;character&lt;/STRONG&gt;. But, when in the next file the value is not missing and it is a &lt;STRONG&gt;numeric&lt;/STRONG&gt; variable, it becomes impossible to concatenate the two files.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Variable activity_first_time_value has been defined as both character and numeric.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;For example 'activity_first_time_value' in the file 'ATTR_2017_3' is &lt;STRONG&gt;characteristic&lt;/STRONG&gt;, while in 'ATTR_2017_4' it is &lt;STRONG&gt;numeric&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="c1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21684i9CE6ACB57196246E/image-size/large?v=v2&amp;amp;px=999" role="button" title="c1.PNG" alt="c1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="c2.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21685iE3C7CA9DEB44E5F4/image-size/large?v=v2&amp;amp;px=999" role="button" title="c2.PNG" alt="c2.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Since I have 453 variables and 200 files to concatenate, it is too much work to account for it individually. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;U&gt;So my question is; is there a way the program this concatenate is such a way that whenever SAS &amp;nbsp;detects in the &amp;nbsp;next file the variables as numeric, the variable in the previous file is also changed into numeric? Or another work around?&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;Kind Regards,&lt;/P&gt;&lt;P&gt;Leonard&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: I also consulted &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/can-we-concatenate-a-numeric-and-character-and-make-it-to/td-p/359905" target="_self"&gt;this&lt;/A&gt;&amp;nbsp;page, but as mentioned before, I believe there a too much variables to account for it manually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jul 2018 10:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476725#M286126</guid>
      <dc:creator>Leonard32</dc:creator>
      <dc:date>2018-07-10T10:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate characteristic and numeric variables for multiple files and numerous variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476736#M286127</link>
      <description>&lt;P&gt;Since all those files should have the same structure anyway, you must not use proc import, but a data step, which guarantees consistent structure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc import makes &lt;EM&gt;guesses&lt;/EM&gt; about the structure, and therefore the results cannot be consistent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take the data step from one of your proc import runs from the log and adapt it to your need, then apply it to all input files.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jul 2018 11:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476736#M286127</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-10T11:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate characteristic and numeric variables for multiple files and numerous variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476741#M286128</link>
      <description>&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; already said: you have to use a data-step to import the files to avoid the problems you have. This means, of course, that you have to name all variables including type and length. Seems to be an annoying job, but it is by far easier, than solving the conflicts created by proc import.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the specification in a text-file, you could import that one and use sas to create the importing data-step.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jul 2018 11:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476741#M286128</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-10T11:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate characteristic and numeric variables for multiple files and numerous variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476849#M286129</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;&lt;SPAN&gt;Since I have 453 variables and 200 files to concatenate, it is too much work to account for it individually. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;So my question is; is there a way the program this concatenate is such a way that whenever SAS &amp;nbsp;detects in the &amp;nbsp;next file the variables as numeric, the variable in the previous file is also changed into numeric? Or another work around?&lt;/U&gt;&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're going to have to write the data step but just once. Once you have it working you can use a wild card or macro approach. After your program for one file is written, the number of files or observations becomes irrelevant. An efficient way is to use PROC IMPORT, copy the code from the log and modify it to be what you need.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have any sort of naming conventions for your files or are they all in one folder? This type of approach may work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jul 2018 15:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476849#M286129</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-10T15:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate characteristic and numeric variables for multiple files and numerous variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476865#M286130</link>
      <description>&lt;P&gt;Read all of the files in one data step and avoid the problems caused by letting SAS guess at how your variables are defined.&lt;/P&gt;
&lt;P&gt;If it is too hard for you make decisions about whether column 212 should be read as numeric or character then why not read them all as character?&amp;nbsp; You can use wildcard in the filename and then test when you start an new file so you can skip the header rows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data attributes_2017;
  length var1-var453 $50 ;
  length filename $200 ;
  infile "$FSROOT/.../attributes.long.55769e3d9a318c4312000009.csv/part-0000*" 
       dsd dlm=',' filename=filename truncover 
  ;
  input @ ;
  if filename ne lag(filename) then input ;
  input var1-var453 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;You can make it better by using the real variable names and types/lengths in the LENGTH statement. For the INPUT statement just use positional variable list.&amp;nbsp; input firstvar -- lastvar ;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jul 2018 16:41:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-characteristic-and-numeric-variables-for-multiple/m-p/476865#M286130</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-10T16:41:16Z</dc:date>
    </item>
  </channel>
</rss>

