BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jagadeesh2907
Obsidian | Level 7

I have a input text file which has data in the below format: 

0|FILE|ENV|p1|p2

1|CARD|PROD|GER|SW
1|CARD|DEV|GER|SW
1|CARD|PROD|GER|VP
9||22/10/2018|3

 

The record with 0 is header, 1 is the information and 9 is footer. I used the below code to read this file but it does not read the data correctly. I need to read the trailer for the information and use them but can drop header and trailer variables at the end. Could you please help: 

DATA TEST;
INFILE File1 DLM="|" DSD ;

INPUT @1 RECORD_TYPE 1.;

IF RECORD_TYPE = 0 THEN DO;
CNT_HDR +1;
OUTPUT;
END;

ELSE IF RECORD_TYPE=9 THEN DO;
INPUT PROCESS_NAME :$4.
LOAD_DATE :DATE9.
NUMBER_OF_RECORDS :1.;

CNT_TLR+1;
OUTPUT;

END;

ELSE DO;
INPUT @2 SYSTEM :$4.
ENVIRONMENT :$4.
PORTFOLIO :$3.
SUBPORTFOLIO :$2.
TOT_CNT+1;
OUTPUT;
END;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
  • you don't need to shout at us or the SAS interpreter
  • post code in a code posting window, or it gets mangled
  • use indentation to create easily readable code
  • use a row hold (@) after reading the record type
  • no informats needed for simple numbers
  • no positions needed when reading delimited files
  • instead of "if-then-else if" use a select() block
  • use the proper date informat on input
  • assign a proper date format for readability
data test;
infile cards dlm="|" dsd;
input record_type @;
select (record_type);
  when (0)do; 
    cnt_hdr + 1;
    output;
  end;
  when (9) do;
    input
      process_name :$4.
      load_date :ddmmyy10.
      number_of_records
    ;
    cnt_tlr + 1;
    output;
  end;
  otherwise do;
    input
      system :$4.
      environment :$4.
      portfolio :$3.
      subportfolio :$2.
    ;
    tot_cnt + 1;
    output;
  end;
end;
format load_date ddmmyy10.;
cards;
0|FILE|ENV|p1|p2
1|CARD|PROD|GER|SW
1|CARD|DEV|GER|SW
1|CARD|PROD|GER|VP
9||22/10/2018|3
;
run;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just read in the file as a pipe delimited file, then you will have a variable where you can do conditional logic to see if its 0 or 9?  I can't actually read that code as its all in upper case, doesn't use a code window (its the {i} above post area), and has smileys in it and such like.

Kurt_Bremser
Super User
  • you don't need to shout at us or the SAS interpreter
  • post code in a code posting window, or it gets mangled
  • use indentation to create easily readable code
  • use a row hold (@) after reading the record type
  • no informats needed for simple numbers
  • no positions needed when reading delimited files
  • instead of "if-then-else if" use a select() block
  • use the proper date informat on input
  • assign a proper date format for readability
data test;
infile cards dlm="|" dsd;
input record_type @;
select (record_type);
  when (0)do; 
    cnt_hdr + 1;
    output;
  end;
  when (9) do;
    input
      process_name :$4.
      load_date :ddmmyy10.
      number_of_records
    ;
    cnt_tlr + 1;
    output;
  end;
  otherwise do;
    input
      system :$4.
      environment :$4.
      portfolio :$3.
      subportfolio :$2.
    ;
    tot_cnt + 1;
    output;
  end;
end;
format load_date ddmmyy10.;
cards;
0|FILE|ENV|p1|p2
1|CARD|PROD|GER|SW
1|CARD|DEV|GER|SW
1|CARD|PROD|GER|VP
9||22/10/2018|3
;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2888 views
  • 1 like
  • 3 in conversation