02-12-2016 02:18 PM
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 ;
variable 1 $3.0
variable 2 8.0
variable 1 $
New_Variable = variable1 !! variable 2 !! variable 3 ;
How do I change/modify the New_Variable legth to be $12 in my output?
02-12-2016 04:09 PM
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.
02-12-2016 04:54 PM
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.
02-16-2016 12:35 PM - edited 02-16-2016 12:36 PM
Thank you, in fact this is what had happended:
Removing one extra character (') from each variable, and then using concatenation.
New_Variable = trim(C_ST) !! trim(C_DSTRB_AREA) !! trim(N_RSK) ;
Need further help from the community? Please ask a new question.