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

I am creating a new variable combining three different variables (all are CHARACTER) with the function below within a DATA Step. The result is a CHARACTER variable with $15.


infile "/v_share/ali/sas_dataset/Calculations_Occupancy" linesize = 300 firstobs = 2  DSD ;

 

Informat

variable 1    $3.0

variable 2     8.0

.

.

 

input

variable 1    $

variable 2    

.

.

New_Variable = variable1 !! variable 2 !! variable 3    ;

 

run;

 

How do I change/modify the New_Variable legth to be $12 in my output?

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
Issue a length statement prior to the assignment.
Data never sleeps

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Issue a length statement prior to the assignment.
Data never sleeps
FreelanceReinh
Jade | Level 19

Hello @almmotamedi,

 

I take it that your code example is only similar to what your real code looks like (blanks in variable names, ...). But I would still be concerned about possible truncation issues. You wrote that all three variables are character variables (the numeric informat 8.0 suggests something else, though). If the default length of New_Variable is 15, this must be the sum of the lengths of the three character variables. (A numeric variable would contribute a length of 12 from the BEST12. format it would be converted with.)

 

The concatenation with the "!!" operator does not trim trailing (or leading) blanks. So, if (for example) 3 and 8 were the lengths of character variables 1 and 2 (hence 15-(3+8)=4 was the length of variable 3), specifying length New_Variable $12; would leave only 1 character for variable 3, irrespective of the contents of the first two variables. Hence, the last three characters of variable 3 would always be truncated.

almmotamedi
Obsidian | Level 7
Its in fact :

New_Variable = trim(variable 1) !! trim(variable 2) !! trim(variable 3)
FreelanceReinh
Jade | Level 19

Good. But you still have to be sure that the total number n of trimmed trailing blanks is at least 3 for each observation, so that the length 15-n of the concatenated string is <=12. Leading blanks would be preserved.

almmotamedi
Obsidian | Level 7

Thank you, in fact this is what had happended:

 

Removing one extra character (') from each variable, and then using concatenation.

 

C_ST =compress(C_ST,"'");
C_DSTRB_AREA =compress(C_DSTRB_AREA,"'");
N_RSK =compress(N_RSK,"'");  


New_Variable = trim(C_ST) !! trim(C_DSTRB_AREA) !! trim(N_RSK) ;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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