BookmarkSubscribeRSS Feed
santhosh
Fluorite | Level 6

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  

 

4 REPLIES 4
Kurt_Bremser
Super User

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.

santhosh
Fluorite | Level 6

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

 

 

Ksharp
Super User
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 3909 views
  • 2 likes
  • 3 in conversation