DATA Step, Macro, Functions and more

Reading particular line from text files

Reply
Frequent Contributor
Posts: 89

Reading particular line from text files

Hello all,

 

I need help

 

i want to read a particular lines from text files 

 

eg: Text file is 

/*************************************************************/

Name : XYZ

phone : 123564

product : DACXA

 

NY

   prod          one   two   three four

shoe     26     25      45     65

AC        56     89      56     87

Stat       63     45      23     54

/*************************************************************/

,

 

 

i need to create a data set with name,Product city and three columns

 

Name  product city prod  three

XYZ    DACXA NY  shoe 45

XYZ    DACXA NY   AC   56  

XYZ    DACXA NY   stat  23

 

 

can anyone help  

 

Super User
Posts: 9,611

Re: Reading particular line from text files

My suggestion:

data want;
length
  line $80.
  name product city prod $10
;
retain name product city;
input;
line = _infile_;
if substr(line,1,2) ne '/*' and line > ' ' and substr(line,4,4) ne 'prod';
if scan(line,1) = 'Name' then name = scan(line,3);
else if scan(line,1) = 'product' then product = scan(line,3);
else if countw(line) = 5
then do;
  prod = scan(line,1);
  three = input(scan(line,4),best.);
  output;
end;
else if substr(line,1,1) ne ' ' then city = line;
drop line;
cards;
/*************************************************************/
Name : XYZ
phone : 123564
product : DACXA
 
NY
   prod          one   two   three four
shoe     26     25      45     65
AC        56     89      56     87
Stat       63     45      23     54
/*************************************************************/
;
run;

proc print data=want noobs;
run;

Result:

name    product    city    prod    three

XYZ      DACXA      NY     shoe      45 
XYZ      DACXA      NY     AC        56 
XYZ      DACXA      NY     Stat      23 

Please use the {i} icon in the future for posting textual data, to prevent accidental formatting by the forum SW.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 89

reading select data from text

Hello,

 

Can any one help me in reading select data from text files

 

 

eg:

 

Name : XYZ

EMPID:9087

PIN:1234

 

CITY : NY

 

PROD SALE1 SALE2 SALE3

DVD   21   32  65

TV      25   35   65

 

i want to read only few columns output

 

EMPID  CIYT SALE3

9087  NY 65

9087  NY 65

 

 

Super User
Posts: 9,611

Re: reading select data from text

Adapt my code. The basic logic is the same.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,621

Re: reading select data from text

[ Edited ]
data x1;
infile 'c:\temp\temp.txt' ;
input @'EMPID' empid & $20. @'CITY' city & $20.;
run;
data x2;
infile 'c:\temp\temp.txt' firstobs=8 truncover;
input PROD : $20. SALE1 SALE2 SALE3;
run;
data want;
 set x2;
 if _n_=1 then set x1;
empid=scan(empid,-1,':');
city=scan(city,-1,':');
run;
Ask a Question
Discussion stats
  • 4 replies
  • 232 views
  • 2 likes
  • 3 in conversation