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

Hey everyone, I want to add a suffix to all data in a column,

 

 

data have;
INPUT ID $;
DATALINES;
AZ1
AZ2
AZ3
;
RUN;

I'm trying to make the data look like this

ID
AZ1_22 AZ2_22 AZ3_22

I would appreciate any help, thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Since you already have a variable adding additional characters may mean that the suffix/prefix could make the new value too long to fit in the existing variable. So in a new data set you need to do something to make the length long enough to hold the largest expected value.

One way using a Length statement:

data have;
/* read with a fixed 3 characters to create that length*/
INPUT ID $3.;
DATALINES;
AZ1
AZ2
AZ3
;
RUN;

data want;
   /*this length statement adds 3 characters to hold the suffix*/
   length id $ 6.;
   set have;
   id = cats(id,'_22');
run;

The CATS function will remove trailing spaces from values and concatenate values from left to right into a new value.

View solution in original post

1 REPLY 1
ballardw
Super User

Since you already have a variable adding additional characters may mean that the suffix/prefix could make the new value too long to fit in the existing variable. So in a new data set you need to do something to make the length long enough to hold the largest expected value.

One way using a Length statement:

data have;
/* read with a fixed 3 characters to create that length*/
INPUT ID $3.;
DATALINES;
AZ1
AZ2
AZ3
;
RUN;

data want;
   /*this length statement adds 3 characters to hold the suffix*/
   length id $ 6.;
   set have;
   id = cats(id,'_22');
run;

The CATS function will remove trailing spaces from values and concatenate values from left to right into a new value.

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
  • 1 reply
  • 2145 views
  • 0 likes
  • 2 in conversation