BookmarkSubscribeRSS Feed
Nietzsche
Lapis Lazuli | Level 10
data temp2;
	set spg.temp (keep=dept site);
Assignment = dept||put(site,2.);
run;
proc print data=temp2;run;

Nietzsche_0-1670755922408.png

Hello, just wondering why is there a space inside the concatenated variable?

 

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
6 REPLIES 6
Astounding
PROC Star
Trailing blanks are already part of DEPT. Try a tool that removes leading and trailing blanks:

assignment = cats(dept, site);
ger15xxhcker
Quartz | Level 8

The space is inside the variable because you specified the format when creating the variable (put(site,2.)). This specifies that the variable should have a length of two, and the space will be included in this length. To remove the space, you can reduce the format to 1. instead of 2.

andreas_lds
Jade | Level 19

@ger15xxhcker wrote:

The space is inside the variable because you specified the format when creating the variable (put(site,2.)). This specifies that the variable should have a length of two, and the space will be included in this length. To remove the space, you can reduce the format to 1. instead of 2.


Sorry, but this is completely wrong. The blanks are part of dept and the html-output collapses the blanks automatically. Here is the log-output of an extended version of the program displaying all blanks:

 81         data temp2;
 82            set sasuser.temp (keep=dept site);
 83            dept = translate(dept, '#', ' ');
 84            Assignment = dept||put(site,2.);
 85            put Assignment=;
 86         run;
 
 Assignment=DP###26
 Assignment=PURH#57
 Assignment=PERS#34
 Assignment=BK###57
 Assignment=DP###95
 Assignment=BK###44
 Assignment=DP###59
 Assignment=PUB##38
 Assignment=DP###44
 Assignment=DP###90
Tom
Super User Tom
Super User

SAS only has two types of variables. Floating point numbers and fixed length character strings.

 

The DEPT variable must be defined as longer than 4 bytes or else there would not be any spaces after the values 'PURH' and 'PERS'.

 

The ODS output results typically use proportional fonts and other changes that make seeing spaces hard.

If you look at the values using fixed width font then it will be clearer what is going on as the append digits will all appear in the same character position.

 

If you do not want to include the spaces used to fill out the fixed length variable then modify your code to remove them.

Assignment = trim(dept)||put(site,2.);
Assignment = cats(dept,site);

Also note that if SITE is less than <10 then the PUT() function will also introduce a space.  You could use Z2. format instead and get strings like 08.  Or use the -L format modifier to left align the digits. So '8 ' instead of ' 8'.

 

Patrick
Opal | Level 21

Below two ways how to do this. If using cats() - most people would - make sure that you define the length of the assignment variable beforehand as else it will default to a length of $200.

data sample;
  length dept $4 site 8 assignment_1 assignment_1 $6;
  dept='ABC';
  site=5;
  assignment_1=strip(dept)||put(site,2. -l);
  assignment_2=cats(dept,put(site,2.));
run;

proc print data=sample;
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
  • 6 replies
  • 526 views
  • 1 like
  • 7 in conversation