Reading raw file data and writing to another raw file data?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Reading raw file data and writing to another raw file data?

I need to import raw file i.e a fixed length  text file, do some processing and write out to another raw file based on a condition without creating a SAS dataset.

My understanding how to do is:

data _null_;

infile 'rawfile' ;

input @1 column1 @5 column2 @12 column3....and so on ;

/* process all data*/

file 'outfile' ;

if (true expression on a condition) then put @1 column1 @5 column...... ;

run;

Is the above the correct method to write the code or am i missing something in my understanding.

A sample record from the raw file looks like:

00000011 M 07/24/2013 AMR Keele Luroy Mark 35 Mega Drive Camarillo CA 93010 USA 888/888-8888 N1971 N N N


Accepted Solutions
Solution
‎06-28-2015 04:13 AM
Super User
Posts: 3,112

Re: Reading raw file data and writing to another raw file data?

What do you mean by "not writing correctly"? What does the SAS log report? If your records are wrapping around to a new line then adding an LRECL = 1200 to your FILE statement may help.

View solution in original post


All Replies
Super User
Posts: 10,533

Re: Reading raw file data and writing to another raw file data?

Basic approach is correct. Details in the paths for the Infile and File statements of course.

You might investigate the _INFILE_ variable to save a bit of coding. You only need to read as variables only the columns you need for your condition. That is one of the lovely things about fixed column data. Then you have:

if (true) then put _infile_;

Use _infile_ reduces worry on the output side of variable types, formats and what gets put without specification.

The default behavior if you read a value such as 45.00 and the put without specifying a format you would most likely get 45. Which has potential for confusion down the road.

Occasional Contributor
Posts: 11

Re: Reading raw file data and writing to another raw file data?

Thank you This is the code I am working with. It reads fine if i mention a dataset name to check whilst I wanna keep it _null_.. However the same code is not writing to the external write correctly. I am not even doing any processing at all and  have no idea where am i going wrong. Can you suggest the corrective action plz?

data null;

infile datalines firstobs=3;

Input @1   in_client_ref_num    $char12.  /* ID                 */

         @13  in_gender            $char1.   /* Gender             */

         @14  raw_dob              $char10.  /* DOB                */

         @24  raw_aff_start_date   $char10.  /* Address Start Date */

         @34  raw_aff_status_code  $char1.   /* aff-status         */

         @35  in_title             $char14.  /* Salutation         */

         @49  in_first_name        $char31.  /* First Name         */

         @80  in_middle_name       $char31.  /* Middle Name        */

         @111 in_last_name         $char31.  /* Last Name          */

         @142 in_suffix            $char16.  /* Suffix             */

         @158 na_spouse_id         $char14.  /* Spouse ID          */

         @172 na_spouse_prefix     $char17.  /* Spouse Prefix      */

         @189 na_spouse_fname      $char31.  /* Spouse First Name  */

         @220 na_spouse_mname      $char31.  /* Spouse Middle Name */

         @251 na_spouse_lname      $char31.  /* Spouse Last Name   */

         @282 na_spouse_suffix     $char16.  /* Suffix             */

         @298 raw_addr_class_code  $char17.  /* Address Class Code */

         @315 in_address_line1     $char50.  /* Home Address 1     */

         @365 in_address_line2     $char50.  /* Home Address 2     */

         @415 in_address_line3     $char50.  /* Home Address 3     */

         @465 tmp_address_line4    $char50.  /* Home Address 4     */

         @515 in_city_name         $char30.  /* Home City          */

         @545 raw_state_code1      $char13.  /* Home State         */

         @558 in_postal_code       $char14.  /* Home Zip           */

         @572 raw_country_desc1    $char15.  /* Home Country       */

         @587 in_phone_num1        $char25.  /* Home Phone         */

         @612 raw_address_line1    $char55.  /* Home Address 1     */ 

         @667 raw_address_line2    $char55.  /* Home Address 2     */ 

         @722 raw_address_line3    $char55.  /* Home Address 3     */ 

         @777 raw_address_line4    $char55.  /* Home Address 4     */ 

         @832 raw_city_name        $char30.  /* Home City          */ 

         @862 raw_state_code2      $char13.  /* Home State         */ 

         @875 raw_postal_code      $char14.  /* Home Zip           */ 

         @889 raw_country_desc2    $char15.  /* Home Country       */ 

         @904 raw_phone_num1       $char25.  /* Home Phone         */ 

         @929 in_particip_code     $char1.   /* Address EFF Status */

         @930 tmp_degree_date1     $char19. 

         @949 in_vip               $char13.

         @962 raw_presence_of_email $char26.

         @988 tmp_degree_date10     $char13.

         @1001 tmp_degree_date9    $char12.

         @1013 tmp_degree_date8    $char12.

         @1025 tmp_degree_date7    $char12.

         @1037 tmp_degree_date6    $char12.

         @1049 tmp_degree_date5    $char12.

         @1061 tmp_degree_date4    $char12.

         @1073 tmp_degree_date3    $char12.

         @1085 tmp_degree_date2    $char12.

         @1097 raw_paying_mbr      $char14. ;

    file 'C:\Users\god\Desktop\output.txt';

   put @1   in_client_ref_num    $char12.  /* ID                 */

         @13  in_gender            $char1.   /* Gender             */

         @14  raw_dob              $char10.  /* DOB                */

         @24  raw_aff_start_date   $char10.  /* Address Start Date */

         @34  raw_aff_status_code  $char1.   /* aff-status         */

         @35  in_title             $char14.  /* Salutation         */

         @49  in_first_name        $char31.  /* First Name         */

         @80  in_middle_name       $char31.  /* Middle Name        */

         @111 in_last_name         $char31.  /* Last Name          */

         @142 in_suffix            $char16.  /* Suffix             */

         @158 na_spouse_id         $char14.  /* Spouse ID          */

         @172 na_spouse_prefix     $char17.  /* Spouse Prefix      */

         @189 na_spouse_fname      $char31.  /* Spouse First Name  */

         @220 na_spouse_mname      $char31.  /* Spouse Middle Name */

         @251 na_spouse_lname      $char31.  /* Spouse Last Name   */

         @282 na_spouse_suffix     $char16.  /* Suffix             */

         @298 raw_addr_class_code  $char17.  /* Address Class Code */

         @315 in_address_line1     $char50.  /* Home Address 1     */

         @365 in_address_line2     $char50.  /* Home Address 2     */

         @415 in_address_line3     $char50.  /* Home Address 3     */

         @465 tmp_address_line4    $char50.  /* Home Address 4     */

         @515 in_city_name         $char30.  /* Home City          */

         @545 raw_state_code1      $char13.  /* Home State         */

         @558 in_postal_code       $char14.  /* Home Zip           */

         @572 raw_country_desc1    $char15.  /* Home Country       */

         @587 in_phone_num1        $char25.  /* Home Phone         */

         @612 raw_address_line1    $char55.  /* Home Address 1     */ 

         @667 raw_address_line2    $char55.  /* Home Address 2     */ 

         @722 raw_address_line3    $char55.  /* Home Address 3     */ 

         @777 raw_address_line4    $char55.  /* Home Address 4     */ 

         @832 raw_city_name        $char30.  /* Home City          */ 

         @862 raw_state_code2      $char13.  /* Home State         */ 

         @875 raw_postal_code      $char14.  /* Home Zip           */ 

         @889 raw_country_desc2    $char15.  /* Home Country       */ 

         @904 raw_phone_num1       $char25.  /* Home Phone         */ 

         @929 in_particip_code     $char1.   /* Address EFF Status */

         @930 tmp_degree_date1     $char19. 

         @949 in_vip               $char13.

         @962 raw_presence_of_email $char26.

         @988 tmp_degree_date10     $char13.

         @1001 tmp_degree_date9    $char12.

         @1013 tmp_degree_date8    $char12.

         @1025 tmp_degree_date7    $char12.

         @1037 tmp_degree_date6    $char12.

         @1049 tmp_degree_date5    $char12.

         @1061 tmp_degree_date4    $char12.

         @1073 tmp_degree_date3    $char12.

         @1085 tmp_degree_date2    $char12.

         @1097 raw_paying_mbr      $char14. ;

   datalines;

CRS_ID      GDT_BIRTH  ADDR_DATE SNAME_PREFIX   FIRST_NAME                     MIDDLE_NAME                    LAST_NAME                      NAME_SUFFIX     SPOUSE_ID     SP_NM_PREFIX     SP_FIRST_NM                    SP_MIDDLE_NM                   SP_LAST_NM                     SP_NM_SUFFIX    ADDRESS_CD       HOME_ADDRESS1                                     HOME_ADDRESS2                                     HOME_ADDRESS3                                     HOME_ADDRESS4                                     HOME_CITY                     HOME_STATE   HOME_POSTAL   HOME_COUNTRY   HOME_PHONE               PREF_ADDRESS1                                          PREF_ADDRESS2                                          PREF_ADDRESS3                                          PREF_ADDRESS4                                          PREF_CITY                     PREF_STATE   PREF_POSTAL   PREF_COUNTRY   PREF_PHONE               AYEAR_OF_GRADUATION DONOR_STATUS PRESENCE_OF_EMAIL_ADDRESS CLASS_YEAR10 CLASS_YEAR9 CLASS_YEAR8 CLASS_YEAR7 CLASS_YEAR6 CLASS_YEAR5 CLASS_YEAR4 CLASS_YEAR3 CLASS_YEAR2 MEMBER_STATUS

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                       

00000018    M          07/24/2013AMR            Tik                            Royel                          Mack                                                                                                                                                                                                        35 Mesa Drive                                                                                                                                                                                           Camarillo                     CA           93010         USA            805/482-2295                                                                                                                                                                                                                                                                                                                                          N1971               N            N                                                                                                                                      N

00000067    M          08/20/2009AMR            Daniel                         jackie                         Smith                                          06203355      MS               Patricia                                                      Fresh                                                           11774 Quail Rd                                                                                                                                                                                          Longmont                      CO           80501-8959    USA                                                                                                                                                                                                                                                                                                                                                                  N1972               N            N                                                                                                                                      N

00000273    M          08/22/2013AMR            Bobby                          E.                             Ferguson                                       04370272      MRS              Diana                          Maria                          Ferguson                                                        16236 Wayfarer Lane                                                                                                                                                                                     Huntington Beach              CA           92649-2100    USA            714/840-7160                                                                                                                                                                                                                                                                                                                                          N1971               N            Y                         1979          

;

Solution
‎06-28-2015 04:13 AM
Super User
Posts: 3,112

Re: Reading raw file data and writing to another raw file data?

What do you mean by "not writing correctly"? What does the SAS log report? If your records are wrapping around to a new line then adding an LRECL = 1200 to your FILE statement may help.

Occasional Contributor
Posts: 11

Re: Reading raw file data and writing to another raw file data?

Hi Yes exactly, That was my problem and it got resolved beautifully with your solution. Can you please offer an explanation for L RECL= and how that works so that I avoid asking such questions again. Smiley Happy I did check online documentation but I am unable to comprehend the concept thoroughly well.  Thank you!

Super User
Posts: 3,112

Re: Reading raw file data and writing to another raw file data?

Glad to be able to help. Please note that your input statement goes out past column position 1097. That must mean that the length of each record must be greater than 1097 characters.

The "LECL" option sets the record length for your file. I just chose 1200 to make sure it was long enough. Probably 1097 + 14 = 1111 would have been enough.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 358 views
  • 0 likes
  • 3 in conversation