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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.