BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nketata
Obsidian | Level 7

 

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

2 REPLIES 2
art297
Opal | Level 21

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

Astounding
PROC Star

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 718 views
  • 1 like
  • 3 in conversation