DATA Step, Macro, Functions and more

How to modify variable length which has already been set?

Accepted Solution Solved
Reply
Contributor
Posts: 72
Accepted Solution

How to modify variable length which has already been set?

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?


Accepted Solutions
Solution
‎02-12-2016 02:38 PM
Super User
Posts: 5,428

Re: How to modify variable length which has already been set?

Posted in reply to almmotamedi
Issue a length statement prior to the assignment.
Data never sleeps

View solution in original post


All Replies
Solution
‎02-12-2016 02:38 PM
Super User
Posts: 5,428

Re: How to modify variable length which has already been set?

Posted in reply to almmotamedi
Issue a length statement prior to the assignment.
Data never sleeps
Trusted Advisor
Posts: 1,117

Re: How to modify variable length which has already been set?

Posted in reply to almmotamedi

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.

Contributor
Posts: 72

Re: How to modify variable length which has already been set?

Posted in reply to FreelanceReinhard
Its in fact :

New_Variable = trim(variable 1) !! trim(variable 2) !! trim(variable 3)
Trusted Advisor
Posts: 1,117

Re: How to modify variable length which has already been set?

Posted in reply to almmotamedi

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.

Contributor
Posts: 72

Re: How to modify variable length which has already been set?

[ Edited ]
Posted in reply to FreelanceReinhard

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) ;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 266 views
  • 1 like
  • 3 in conversation