DATA Step, Macro, Functions and more

reading txt file or datalines

Accepted Solution Solved
Reply
Super Contributor
Posts: 266
Accepted Solution

reading txt file or datalines

data test;

infile datalines missover;

input x y$ z;

datalines;

34     aman 56

56 vikash 34

34   aman 56

56     vikash 34

34   aman

56              34

          aman 56

56   vikash 34

34   aman 56

56 vikash

34      56

56 vikas

;

run;

proc print; run;

please let me know how i can read records in correct format...


Accepted Solutions
Solution
‎09-03-2013 09:54 AM
PROC Star
Posts: 7,491

Re: reading txt file or datalines

Then try:

data test;

  infile datalines truncover;

  input x 1-2;

  _infile_=substr(_infile_,3);

  y=scan(_infile_,1);

  if not anyalpha(y) then do;

    call missing(y);

    z=input(scan(_infile_,1),8.);

  end;

  else z=input(scan(_infile_,2),8.);

  datalines;

34     aman 56

56 vikash               34

34   aman 56

56     vikash 34

34   aman

56              34

          aman       56

56   vikash 34

34   aman 56

56 vikash

34      56

56 vikas

;

View solution in original post


All Replies
PROC Star
Posts: 7,491

Re: reading txt file or datalines

At least for your example, the following would work:

data test;

  infile datalines truncover;

  input @;

  _infile_=tranwrd(_infile_,'        ',' ');

  _infile_=tranwrd(_infile_,'      ',' . ');

  input x 1-2 y $ z;

  datalines;

34     aman 56

56 vikash 34

34   aman 56

56     vikash 34

34   aman

56              34

          aman 56

56   vikash 34

34   aman 56

56 vikash

34      56

56 vikas

;

Super Contributor
Posts: 266

Re: reading txt file or datalines

Thank you very much for your kind help.

sir i would like to add one thing that your ans is suitable for only this sample data in exact this format. but  if i will change place of any field value(means adding one or more space in between) so it will give wrong ans. which is possible to come in complete population.

i tried something but it is not working in name field.

data test;

  infile datalines truncover dlm=",";

  input @;

  _infile_=tranwrd(_infile_,' ',",");

  input x 1-2 y $ z;

  datalines;

34     aman 56

56 vikash               34

34   aman 56

56     vikash 34

34   aman

56              34

          aman       56

56   vikash 34

34   aman 56

56 vikash

34      56

56 vikas

;

run;

Kindly suggest me ....

Solution
‎09-03-2013 09:54 AM
PROC Star
Posts: 7,491

Re: reading txt file or datalines

Then try:

data test;

  infile datalines truncover;

  input x 1-2;

  _infile_=substr(_infile_,3);

  y=scan(_infile_,1);

  if not anyalpha(y) then do;

    call missing(y);

    z=input(scan(_infile_,1),8.);

  end;

  else z=input(scan(_infile_,2),8.);

  datalines;

34     aman 56

56 vikash               34

34   aman 56

56     vikash 34

34   aman

56              34

          aman       56

56   vikash 34

34   aman 56

56 vikash

34      56

56 vikas

;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 278 views
  • 3 likes
  • 2 in conversation