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

data out;

   set in;

    val='bob ';

    val=val || middle_name;

    val= val || last_name;

 

How to make this build up over several lines? Thanks!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
*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!

 

 


 

View solution in original post

4 REPLIES 4
ballardw
Super User

@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.

Reeza
Super User
*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
Quartz | Level 8

replacing pipes with catx is probably the gem of learning. Thanks both of you!

Shmuel
Garnet | Level 18

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 770 views
  • 0 likes
  • 4 in conversation