BookmarkSubscribeRSS Feed
ACLAN
Obsidian | Level 7

Hi,

 

I'm using the following code to calculate time difference between 2 variables:

 

 

/* create new columns to calculate time differences and convert Time1 Time2 Time3 Time4 variables (columns have character values)  */
data Ttimeslots2;
set Ttimeslots;
NewDraw1diff =  input(Time1,time8.);
NewDraw2diff =  input(Time2,time8.);
NewDraw3diff =  input(Time3,time8.);
NewDraw4diff =  input(Time4,time8.);
format NewDraw1diff NewDraw2diff NewDraw3diff NewDraw4diff time8.;
run;

 

I have checked the variables attributes and the variables NewDraw1diff, NewDraw2diff, NewDraw3diff, and NewDraw4diff have time8. as format . 

 

Due to as error in variable Time1 in the original dataset, I have to correct the values in NewDraw1diff if a given condition is met based on the following code:

 

 

data Ttimeslots3;
set Ttimeslots2;
if index(SP,'18C') then NewDraw1diff=Draw1-EDTIME;
format NewDraw1diff time8.;
run;

Both Draw1 and EDTIME have time8. as format and informat and some rows have missing values. 

 

when I run the code above I get the following log messages:

 

NOTE: Variable Draw1 is uninitialized.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
56 at 400:58
NOTE: There were 1890 observations read from the data set TTIMESLOTS2.
NOTE: The data set TTIMESLOTS3 has 1890 observations and 20 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

 

It seems like the if statement is not property working and I can't figure out why. 

 

Does anyone know what's wrong with my code?

 

 

 

 

 

6 REPLIES 6
Kurt_Bremser
Super User
NOTE: Variable Draw1 is uninitialized.

means that variable Draw1 is not on your input dataset. Did you actually want to use time1? If yes, and the conversion in your first step worked, then your calculation can't work, as time1 would be of type character. Please post the whole log from these two steps (including all the code!) into a window opened with the </> button.

ACLAN
Obsidian | Level 7
Yes, I need to use time1 in step 1 as I do not have data for all rows to calculate the time difference in step 2 based on Draw1 and EDTIME.

The variable time1, which was a character, was used in step 1 and the data are numbers written in hh:mm format. unfortunately, for the rows in which the variable SP has "18C" as a string, the characters are 4860,00000000001 as an example.

Can you please explain why the calculation in step 2 can't work ? the NewDraw1 format was defined as time8. and the Draw1 and EDTIME have the same format.

The datasets Ttimeslots2 and Ttimeslots3 should have the exact same variables. And maybe I need some coffee...
ballardw
Super User

@ACLAN wrote:

Can you please explain why the calculation in step 2 can't work ? the NewDraw1 format was defined as time8. and the Draw1 and EDTIME have the same format.

Calculations depend on the data. Without actual data there really isn't any way to be sure what is going on.

Provide example data in the form of a data step.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against. The code box is important as the message windows will reformat text possibly rendering the data step code invalid.

 

Provide enough examples that duplicate the "problem" cases and some non-problem if possible.

ACLAN
Obsidian | Level 7

@ballardw Thank you for helping by given the link to the example on who to reproduce our data. This is really good for people like me who are just starting to use SAS.

ACLAN
Obsidian | Level 7

 I found the problem. Draw1 was not used because it has a space in the variable name (Draw 1). I changed the code to Draw_1 and not it works. I'm new to SAS and I had no idea that this can be an issue.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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