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;
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
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
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.