DATA Step, Macro, Functions and more

Removing line returns from SAS data file

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Removing line returns from SAS data file

 

Hello, How to get the file  WANT starting from HAVE ?

 

data HAVE;

infile datalines truncover;

input var1 $200.;

datalines;

(S) Benefit

plans

(S) car

allowance

(S) Technology,

Economic Development Initiative

Motor allowance

(S) Small Business and Tourism

Agriculture

(S) Small Business

Financing Act

;

run;

 

DATA WANT;

format var1 $200.;

var1="(S) Benefit plans"; output;

var1="(S) car allowance"; output;

var1="(S) Technology, Economic Development Initiative Motor allowance"; output;

var1="(S) Small Business and Tourism Agriculture"; output;

var1="(S) Small Business Financing Act"; output;

run;

 


Accepted Solutions
Solution
‎06-15-2017 02:40 PM
PROC Star
Posts: 7,366

Re: Removing line returns from SAS data file

Here is one way:

data HAVE;
  infile datalines truncover;
  input var1 $200.;
  datalines;
(S) Benefit
plans
(S) car
allowance
(S) Technology,
Economic Development Initiative
Motor allowance
(S) Small Business and Tourism
Agriculture
(S) Small Business
Financing Act
;
run;

data want (drop=_:);
  length var1 $200;
  retain var1;
  merge have (rename=(var1=_var1_in)) have (firstobs=2 rename=(var1=_var1_next));
  if first(_var1_in) eq '(' then var1=_var1_in;
  else var1=catx(' ',var1,_var1_in);
  if missing(_var1_next) or first(_var1_next) eq '(' then output;
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Solution
‎06-15-2017 02:40 PM
PROC Star
Posts: 7,366

Re: Removing line returns from SAS data file

Here is one way:

data HAVE;
  infile datalines truncover;
  input var1 $200.;
  datalines;
(S) Benefit
plans
(S) car
allowance
(S) Technology,
Economic Development Initiative
Motor allowance
(S) Small Business and Tourism
Agriculture
(S) Small Business
Financing Act
;
run;

data want (drop=_:);
  length var1 $200;
  retain var1;
  merge have (rename=(var1=_var1_in)) have (firstobs=2 rename=(var1=_var1_next));
  if first(_var1_in) eq '(' then var1=_var1_in;
  else var1=catx(' ',var1,_var1_in);
  if missing(_var1_next) or first(_var1_next) eq '(' then output;
run;

Art, CEO, AnalystFinder.com

Super User
Posts: 5,099

Re: Removing line returns from SAS data file

Another way to get from the HAVE data to the WANT data:

 

data want;

length var1_combined $ 200;

do i=1 to 2;

   set have;

   if i=1 then var1_combined = var1;

   else var1_combined = catx(' ', var1_combined, var1);

end;

keep var1_combined;

rename var1_combined = var1;   /* optional */

run;

☑ This topic is solved.

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

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