BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
prolifious
Obsidian | Level 7

Hi,

 

I'm having trouble keeping a date format when concatenating to a char.

 

i want to attach a char variable to yesterday's date, for example:

 

abc_20180416.

 

here is the code I made:

 

data temp0;

yest =today() -1; format yest yymmddn8.;

var1='abc_';

var2= cat(var1,yest);

run;

proc print data=temp0 (keep=yest var2);

run;

 

The result is:

 

yest                     var2

20180416            abc_21290;

 

I've tried a few different things, but can't seem to pin down how to get var2= abc_20180416.  

 

Any tips out there?

 

Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's the statement that creates VAR2:

 

var2= cat(var1,yest);

 

Try it this way instead:

 

var2= cat(var1,put(yest, yymmddn8.));

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Here's the statement that creates VAR2:

 

var2= cat(var1,yest);

 

Try it this way instead:

 

var2= cat(var1,put(yest, yymmddn8.));

prolifious
Obsidian | Level 7

it works.  thank you very much for your help here!

Kurt_Bremser
Super User

The crux behind this:

data temp0;
format yest yymmddn8.;
yest = today() - 1;
var1 = 'abc_';
var2 = cat(var1,yest);
run;

is that yest is a numeric variable, while cat() is a string function; so you force SAS to do an automatic conversion from numeric to character, and in such automatic conversions, no formats are applied, and you get the raw value (days from 1960-01-01 to yesterday) appended.

@Astounding's solution prevents this.

 

Hint: never allow automatic type conversions in your programs (you'll find them by the NOTEs they cause). See my Maxims 31 & 25. Take control.

prolifious
Obsidian | Level 7

Thank you, Kurt.  These maxims are great!  Good reading I'll take up later on.  Thanks again for your direction.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 913 views
  • 2 likes
  • 3 in conversation