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?
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.
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.