<?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 Convert Character to Numeric Time Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520880#M141278</link>
    <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes all of the files have the same structure and variable names. I used a simple PROC IMPORT to upload each one (below).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import
		datafile="pathname\file.csv"
		dbms=csv replace
		out=file_work;
		guessingrows=900;
	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What else would I need to add to import them as the correct format?&lt;/P&gt;</description>
    <pubDate>Wed, 12 Dec 2018 16:06:43 GMT</pubDate>
    <dc:creator>_CJY_</dc:creator>
    <dc:date>2018-12-12T16:06:43Z</dc:date>
    <item>
      <title>Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520858#M141268</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a list of 5 CSV files that do not have uniform formats for its time variables (deptTime and AdmitTime). Some CSV files have both of them in numeric, some both in character, and others are mixed. It becomes an issue when I need to merge all of the 5 into one data set. This seems to be on going issue with no rhyme or reason as to which of the two vars will be in character or numeric format. In order to avoid this issue in the future, I have tried to create a macro to accomplish two things:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Identify whether the time variable is character or numeric (Using the VTYPE function for this)&lt;/P&gt;&lt;P&gt;2. If character, then convert to numeric time&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The end result shows that all of the time variables have been converted to a numeric format (time20.3), but have the all of the values are missing. Any help or suggestions would be greatly appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the Macro that I have tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro timewarp;

data &amp;amp;file._;
set &amp;amp;file (rename=(deptchecktime=deptwarp admitTime=admitwarp));
admit=vtype(admitwarp);
dept=vtype(deptwarp);
if admit="C" then do;
admitTime=input(admitwarp,time20.3);
format admitTime time20.3;
end;
if dept="C" then do;
deptchecktime=input(deptwarp,time20.3);
format deptchecktime time20.3;
end;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance,&lt;/P&gt;&lt;P&gt;Carlos&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS V9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 15:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520858#M141268</guid>
      <dc:creator>_CJY_</dc:creator>
      <dc:date>2018-12-12T15:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520861#M141271</link>
      <description>Are the five files the same structure and variable names? How did you import them? The correct answer here is go back to the import step and import them as the correct type (numeric/character) and format.</description>
      <pubDate>Wed, 12 Dec 2018 15:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520861#M141271</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-12T15:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520864#M141272</link>
      <description>&lt;P&gt;We would need to have access to a portion of your data to determine why you are getting missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide a portion of the data as a SAS data step&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 15:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520864#M141272</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-12-12T15:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520879#M141277</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.FORUM_SAMPLE;
  infile datalines dsd truncover;
  input deptCheckTime:TIME20.3 admitTime:$5.;
  format deptCheckTime TIME20.3;
datalines;
12:09:00.000 10:46
16:56:00.000 17:37
13:31:00.000 1:45
13:33:00.000 13:49
11:50:00.000 21:02
13:41:00.000 23:10
10:50:00.000 14:59
14:41:00.000 10:51
14:41:00.000 10:51
12:35:00.000 22:45
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you for the code to make this easier to understand. I included a small sample where there is a mix of both character and numeric.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520879#M141277</guid>
      <dc:creator>_CJY_</dc:creator>
      <dc:date>2018-12-12T16:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520880#M141278</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes all of the files have the same structure and variable names. I used a simple PROC IMPORT to upload each one (below).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import
		datafile="pathname\file.csv"
		dbms=csv replace
		out=file_work;
		guessingrows=900;
	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What else would I need to add to import them as the correct format?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:06:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520880#M141278</guid>
      <dc:creator>_CJY_</dc:creator>
      <dc:date>2018-12-12T16:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520882#M141279</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245176"&gt;@_CJY_&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a list of 5 CSV files that do not have uniform formats for its time variables (deptTime and AdmitTime). Some CSV files have both of them in numeric, some both in character, and others are mixed. It becomes an issue when I need to merge all of the 5 into one data set. This seems to be on going issue with no rhyme or reason as to which of the two vars will be in character or numeric format. In order to avoid this issue in the future, I have tried to create a macro to accomplish two things:&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;Are you using proc import to read these? Are they supposed to be the same file layout? Then likely you should be using a datastep to read the external file. Proc Import guesses each and every time a file is read. So lengths of variables and types can change depending on the actual content of the first few rows of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step you can specify a specific informat to read the data. If the content changes you may be able to use the Anydtdte, Anydttme or Anydtdtm informats though I would carefully examine the results as some assumptions are made when attempting to read a (garbage format) date such as 101112&amp;nbsp;, which might be 10 Nov 2012, 10 Nov 1912, 11 Oct 2012, 11 Oct 1912, 12 Nov 2010 or 12 Nov 1910.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also ask the data source why the content keeps changing. Maybe someone else needs to clean up a process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your CSV files are coming from a spreadsheet you might also try setting cell properties for the date or time columns to a single standard value type before exporting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are also some issues around the actual length of your character variable and informat used. If your character value has leading spaces the informat may read incorrectly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more specific help it might help to show the actual values of the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW there isn't any need to add new variables just to check the type. You can use&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0000" face="SAS Monospace" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; vtype(admitwarp)=&lt;/FONT&gt;&lt;FONT color="#800080" face="SAS Monospace" size="2"&gt;"C"&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; then admitTime=input(admitwarp,&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;time20.3&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;There is also no reason to include the FORMAT statement in a do block as FORMAT is not executable and is applied wherever it occurs in the code.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520882#M141279</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-12-12T16:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520887#M141281</link>
      <description>&lt;P&gt;PROC IMPORT does not allow you to specify data types and formats. But it will generate the skeleton code for you. Run it once for a file, copy the code from the log. Make sure the types and formats are correct there. Then use that same code to read your other files, this means they'll all have the same structure.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can add GUESSINGROWS=MAX to your PROC IMPORT as well, it will be slower but will do a better job at guessing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245176"&gt;@_CJY_&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes all of the files have the same structure and variable names. I used a simple PROC IMPORT to upload each one (below).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import
		datafile="pathname\file.csv"
		dbms=csv replace
		out=file_work;
		guessingrows=900;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What else would I need to add to import them as the correct format?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:21:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520887#M141281</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-12T16:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520890#M141284</link>
      <description>&lt;P&gt;Balladrw,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your suggestions. All of it was extremely useful. Did not realize FORMAT function would not work inside a do block.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;file._;
		set &amp;amp;file (rename=(deptchecktime=deptwarp admitTime=admitwarp));
	 	if vtype(admitwarp)="C" then admitTime=input(admitwarp,time20.3);
		format admitTime time20.3;
		if vtype(deptwarp)= "C" then deptchecktime=input(deptwarp,time20.3);
	 	format deptcheckTime time20.3;
	drop deptwarp admitwarp;
	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:28:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520890#M141284</guid>
      <dc:creator>_CJY_</dc:creator>
      <dc:date>2018-12-12T16:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to Convert Character to Numeric Time Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520897#M141286</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245176"&gt;@_CJY_&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Balladrw,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for your suggestions. All of it was extremely useful. Did not realize FORMAT function would not work inside a do block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;file._;
		set &amp;amp;file (rename=(deptchecktime=deptwarp admitTime=admitwarp));
	 	if vtype(admitwarp)="C" then admitTime=input(admitwarp,time20.3);
		format admitTime time20.3;
		if vtype(deptwarp)= "C" then deptchecktime=input(deptwarp,time20.3);
	 	format deptcheckTime time20.3;
	drop deptwarp admitwarp;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Format works just fine. The location in the code doesn't matter unless you have two format statements for the same variable in which case the last one encountered is going to apply. See below for brief example.&lt;/P&gt;
&lt;PRE&gt;data work.example;
   format x best4.;
   do;
      x = 1.234;
      format x percent8.3;
   end;
run;
data work.example2;
   format x percent8.3;
   do;
      x = 1.234;
   end;
   format x best4.;
run;
&lt;/PRE&gt;
&lt;P&gt;There are several non-executable statements such as Format, Informat, Attrib, and&amp;nbsp;Label.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 16:41:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-Convert-Character-to-Numeric-Time-Variables/m-p/520897#M141286</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-12-12T16:41:43Z</dc:date>
    </item>
  </channel>
</rss>

