Posts

Showing posts with the label findc
Extracting the First Three Alphabetic Characters in SAS: A Comparative Guide Extracting the First Three Alphabetic Characters in SAS: A Comparative Guide When working with "Agreement" numbers that contain both alphabetic and numeric characters, there are several approaches you can use in SAS to extract the first three alphabetic characters. Below, I’ll discuss three common methods: substr with findc , substr with compress , and substr with left . Each method has its own advantages and disadvantages, depending on the structure of your data and your specific needs. Approach 1: substr with findc char_part = substr(Agreement, 1, findc(Agreement, '0123456789')-1); Explanation: This approach finds the position of the first numeric character in the Agreement string using findc , and then extracts all characters before that position using substr . It works well if the alphabetic part of the "Agreement" is always at the beginnin...

How to convert the datetime character string to SAS datetime value? (ANYDTDTM and MDYAMPM formats)

Image
When we have a string like this "9/01/2010 11:52:54 AM" and would like to translate the string to a numeric SAS date time variable, most of the times we use SCAN function to extract the information to get the DATETIME format. This is definitely a tedious job. SAS formats ( MDYAMPM, ANTDTDTM ) comes to rescue us. Here is how it works. data test ; length date $25 ; date=" 9/01/2010 11:52:54 AM "; *Convert the character string to SAS datetime value; datetimevar = input (date, mdyampm25.2 ); datetimevar1 = input (date, anydtdtm20. ); *Apply format to the SAS date time value; format datetimevar datetimevar1  datetime19. ; run ; Result: 01SEP2010:11:52:54 * ANYDTDTM and MDYAMPM informats work together when the datetime value has AM PM specified or day, month, and year components are not ambiguous. The MDYAMPMw . format writes datetime values with separators in the form mm/dd/yy hh:mm AM PM, and requires a space between the date and the time....