DATA Step, Macro, Functions and more

Read multiple records from a txt file into one value

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Read multiple records from a txt file into one value

[ Edited ]

Hello,

 

I have a txt file that contains multiple equations which are separated by ";". 

Some equations are splited into 2 records because they are very long. 

In the example below eq1 and eq3 are not separated but eq2 is:

 

eq1 = x1*a+ x2*b + x3*c ;

eq2 = x1*a + x2*b +

x3*c + x4*d ; 

eq3 = x1*a + x2*b + x3*c ; 

 

I want to create a dataset with only 1 variable that contains one row for each equations like that : 

VAR1

eq1=eq1 = x1*a+ x2*b + x3*c

eq2 = x1*a + x2*b +x3*c + x4*d

eq3 = x1*a + x2*b + x3*c

 

The basic code to import is : 

 

data temp;
infile "txt_file" dlm=";" ;
input var1 :$1000.;
run;

 

I tried many things but nothing works. Every time SAS creates 2 rows for the splitted equations. I know with this exemple you can just add a data step after the import and concatenate the 2 separeted row for the equation 2 but I cannot do that with my real data.

 

Thank you for your help.

 

 


Accepted Solutions
Solution
‎12-20-2017 10:40 AM
Super User
Posts: 9,560

Re: Read multiple records from a txt file into one value

Try this:

data want;
infile datalines4 truncover;
input equation $400.;
do while (substr(equation,length(equation),1) ne ';');
  input add_line $200.;
  equation = catx(' ',equation,add_line);
end;
drop add_line;
datalines4;
eq1 = x1*a+ x2*b + x3*c ;
eq2 = x1*a + x2*b +
x3*c + x4*d ; 
eq3 = x1*a + x2*b + x3*c ;
;;;;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎12-20-2017 10:40 AM
Super User
Posts: 9,560

Re: Read multiple records from a txt file into one value

Try this:

data want;
infile datalines4 truncover;
input equation $400.;
do while (substr(equation,length(equation),1) ne ';');
  input add_line $200.;
  equation = catx(' ',equation,add_line);
end;
drop add_line;
datalines4;
eq1 = x1*a+ x2*b + x3*c ;
eq2 = x1*a + x2*b +
x3*c + x4*d ; 
eq3 = x1*a + x2*b + x3*c ;
;;;;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 2

Re: Read multiple records from a txt file into one value

Posted in reply to KurtBremser

It works ! Thank you. 

☑ This topic is solved.

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

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