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

Hi All,

Hope all are safe!.

I'm trying to create variable to date9. format using existing variable but not getting any values being populated 

data test2 (keep=order_id order_dt event);
set test;
event =order_dt;
run;

enclosed order_dt and event_dt formatting 

 

mck.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@meckarthik wrote:
Hi Thanks for the swift response. Dataset test does not have event variable. i want to create this variable in new dataset test2. Event variable should take all values from order_dt in date9. format. Apologise if I'm missing something

Your picture showed a variable named Event that was character. Order_dt is a date. The code you showed would not create a character variable. So either you showed us code other than you actually used or your input data set already had Event.

 

Hint: Copy the code and any messages from the log when ever there is a question. Paste the copied text into a code box opened on the forum with the </> icon to preserve formatting. Often error or warnings have some diagnostic characters that point to the problem area. The message windows on this forum will reformat the text making those characters less useful.

 

It is amazing how often we see "I submitted this code and it didn't work". Then when, after several prompts, we finally get the Log version of the code we find that what was actually submitted was anywhere from a minor difference to quit different than the posted example code. So if we see a LOG entry we know what actually was submitted.

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

Maxim 3: Know Your Data. Variable event is already present in your dataset test, and defined as character.

Maxim 2: Read the Log. You will find the conversion NOTE.

ballardw
Super User

Please describe from what to what you are wanting to create.

It appears that your order_dt variable is already a date and formatted as Date9.

 

Your attachment does not show anything related to event_dt, and neither does your code.

 

If the variable Event already exists in your data set, it appears that it is character. Once a variable is created in SAS you cannot change it from character to numeric or the other way. Dates are numeric with special formats to appear readable to humans as dates.

 

If the variable event already exists in your data then try

data test2 (keep=order_id order_dt event);
   set test (drop=event);
   event =order_dt;
   format event date9.;
run;

Which will create a numeric Event variable. Probably better would be to create a completely new variable that won't get confused with the existing event variable, what ever it may hold.

meckarthik
Quartz | Level 8
Hi Thanks for the swift response. Dataset test does not have event variable. i want to create this variable in new dataset test2. Event variable should take all values from order_dt in date9. format. Apologise if I'm missing something
ballardw
Super User

@meckarthik wrote:
Hi Thanks for the swift response. Dataset test does not have event variable. i want to create this variable in new dataset test2. Event variable should take all values from order_dt in date9. format. Apologise if I'm missing something

Your picture showed a variable named Event that was character. Order_dt is a date. The code you showed would not create a character variable. So either you showed us code other than you actually used or your input data set already had Event.

 

Hint: Copy the code and any messages from the log when ever there is a question. Paste the copied text into a code box opened on the forum with the </> icon to preserve formatting. Often error or warnings have some diagnostic characters that point to the problem area. The message windows on this forum will reformat the text making those characters less useful.

 

It is amazing how often we see "I submitted this code and it didn't work". Then when, after several prompts, we finally get the Log version of the code we find that what was actually submitted was anywhere from a minor difference to quit different than the posted example code. So if we see a LOG entry we know what actually was submitted.

meckarthik
Quartz | Level 8

Thank you, got it now

Kurt_Bremser
Super User

Do not post screenshots embedded in Word documents. You can post pictures in your message by using the "Insert Photos" button (looks like a little camera). I edited your post accordingly.

This will make it immediately visible without the need to open the document previewer (which is not possible at many corporate sites because they block cloud applications) or downloading the Word file (again blocked at many sites because of the security risks of such files). I could only view your attachment on my private MacBook, not on my office computer.

 

The red triangular symbol preceding the variable name shows that the variable is of type character. This can only happen with your code as posted if variable event is already present in the input dataset. If it was not present, the code will create the variable as numeric.

Believe a Super User with 20+ years of SAS experience. Been there, done that, bought the T-shirt.

meckarthik
Quartz | Level 8

Thanks for your suggestion. Will do as advised going forward 🙂

 

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