BookmarkSubscribeRSS Feed
jdub
Calcite | Level 5

Hi

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;

yr=substr(year,3,2);

yearquarter=yr||":"||quarter;

run;

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.

4 REPLIES 4
art297
Opal | Level 21

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

yearquarter=strip(yr)||":"||strip(quarter);

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.

Art

jdub
Calcite | Level 5

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

DLing
Obsidian | Level 7

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.

Peter_C
Rhodochrosite | Level 12

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. 

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
  • 4 replies
  • 1599 views
  • 0 likes
  • 4 in conversation