DATA Step, Macro, Functions and more

substr function

Posts: 33

substr function


I have two variables I'd like to concatenate (year, quarter) but using "||" to concatenate the variables leaves a whole bunch of spaces in the created variable.  I am looking to create a year-quarter identifier without the spaces in the new variable.  I would like to create a year-quarter variable like: 00:1 but I am having some trouble.

Here is what I was trying to do to get that variable:

data blah;

set blah2;




In the log there is a line that says: "numeric values have been converted to character values at the places given by: 168:11 169:22.

The yr variable has missing observations and the year quarter variable has what looks to be a tab btwn the : and the quarter identifier. 

Any help would be appreciated.

Posts: 8,167

substr function

You can get rid of the spaces by enclosing the two variables within strip functions.  e.g.,


Do you want a character or numeric variable?  That answer is needed as well as an answer to how you want to deal with missing values.


Posts: 33

substr function

I am using the year-quarter as a time-period identifier, so I think it would be fine if yearquarter is a charcter variable.

thx for the response

Frequent Contributor
Posts: 104

substr function

The conversion message means SAS is expecting a character variable but you've supplied a numeric variable instead.  SAS tries to be helpful and has done automatic type conversion to the best of its abilities, but it may not be what you want.  In general, it is dangerous coding practice to rely upon automatic type conversion.

I can't tell which statement is giving you the conversion message, and I don't know the types of your variables.

1) If yr and qtr are both numeric, use this:  date = compress( put(yr,4.) || ':' || put(qtr,1.) );   the compress call is defensive programming.

2) If yr is character and qtr is numeric, date = compress( yr || ':' || put(qtr,1.) );

In either case, you'd want to control the length of the date variable by "length date $ 6;" somewhere in the data step.

Valued Guide
Posts: 2,191

substr function

I think that you should use the CATS() function.

Without producing messages it will concatenate numeric and character variables after it trims leading and trailing blanks.

Since your numeric needs to be in a date format, you might need to use the vvalue() function or put() to ensure your chosen format is used, rather than the 12. format normally used by SAS when converting numeric variables to handle as strings. 

Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation