data out;
set in;
val='bob ';
val=val || middle_name;
val= val || last_name;
How to make this build up over several lines? Thanks!
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
3 football CA
;
run;
*Sort - required for both options;
proc sort data=have;
by orgID;
run;
**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
set have;
by orgID;
length combined $100.;
retain combined;
if first.orgID then
combined=states;
else
combined=catx(', ', combined, states);
if last.orgID then
output;
run;
**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
by orgID;
var states;
run;
data want_option2;
set wide;
length combined $100.;
combined=catx(', ', of state_:);
run;
Note the use of CAT_ function instead of pipes, it helps reduce issues, ie trailing and leading spaces or wanting to add a delimiter between items.
https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a
@cellurl wrote:
data out;
set in;
val='bob ';
val=val || middle_name;
val= val || last_name;
How to make this build up over several lines? Thanks!
@cellurl wrote:
data out;
set in;
val='bob ';
val=val || middle_name;
val= val || last_name;
How to make this build up over several lines? Thanks!
It may help to provide an example. If you want to combine several "words" it is usually a good idea to specify an overall length for the variable. In your example the variable VAL would have a length of 3 and concatenating other words won't actually work.
Consider assuming that you think the longest value resulting with all the words would be 25 characters including spaces to separate them. Increase the value of 25 if you need longer.
length val $ 25;
val = catx(' ','bob', middle_name, last_name);
would create the val with a single space, the first parameter to the catx function, between bob and the values of middle_name and last_name.
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
3 football CA
;
run;
*Sort - required for both options;
proc sort data=have;
by orgID;
run;
**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
set have;
by orgID;
length combined $100.;
retain combined;
if first.orgID then
combined=states;
else
combined=catx(', ', combined, states);
if last.orgID then
output;
run;
**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
by orgID;
var states;
run;
data want_option2;
set wide;
length combined $100.;
combined=catx(', ', of state_:);
run;
Note the use of CAT_ function instead of pipes, it helps reduce issues, ie trailing and leading spaces or wanting to add a delimiter between items.
https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a
@cellurl wrote:
data out;
set in;
val='bob ';
val=val || middle_name;
val= val || last_name;
How to make this build up over several lines? Thanks!
replacing pipes with catx is probably the gem of learning. Thanks both of you!
It is not clear what you mean by "several lines".
What is your input format. Post a sample of your data.
BTW you can write the lines into one:
data want;
set have;
length new_val $30;
new_val = 'Bob ' || middle_name || last_name;
/* or: new_val = catx(' ', 'Bob',middle_name,last_name); */
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.