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

Hey, so I'm trying to learn SAS from this book called 'Master SAS for data analytics'.

 

I'm stuck with a proposed exercise (Chapter 6, fourth Hands-on example) exercise within this book and I'd appreciate if any of you could help me with it.  Here it goes:

 

DATA MYSASLIB.CLEANED;SET MYSASLIB.MESSYDATA;

LABEL
EDUCATION='Years of Schooling'
HOW_ARRIVED='How Arrived at Clinic'
TOP_REASON='Top Reason for Coming'
SATISFACTION='Satisfaction Score'
Subject="Subject ID"
DateArrived="Date Arrived"
TimeArrive="Time Arrived"
DateLeft="Date Left"
TimeLeft="Time Left"
Married="Married?"
Single="Single?"
Age="Age Jan 1, 2014"
Gender="Gender"
Race="Race"
Satisfaction="Satisfaction Score";
TEMP=ARRIVAL;
DROP ARRIVAL;
LABEL TEMP='Arrival Temperature';

*****************************************************************STEP 5;

FORMAT ARRIVEDT DATETIME18.;
FORMAT DATEARRIVED2 DATE10. TIMEARRIVET TIME8.;
DATEARRIVED2=INPUT(TRIM(DATEARRIVED),MMDDYY10.);
I= FIND(TIMEARRIVE," ");
P=FIND(TIMEARRIVE,"P");
TIMEARRIVE2=SUBSTR(TIMEARRIVE,1,I-1);
TIMEARRIVET=INPUT(TRIM(TIMEARRIVE2),TIME8.);
IF P>0 THEN TIMEARRIVET=TIMEARRIVET+43200;
ARRIVEDT=DHMS(DATEARRIVED2,0,0,TIMEARRIVET) ;

Label ARRIVEDT="Date & Time Arrived";
RUN;

*****************************************************************STEP 6;
FORMAT LEFT_DT DATETIME18.;
FORMAT DATELEFT_2 DATE10. TIMELEFT_T TIME8.;
DATELEFT_2 = INPUT(TRIM(DATELEFT), MMDDYY10.);
K = FIND(TIMELEFT, ' ');
L= FIND(TIMELEFT, 'P');

TIMELEFT_2= SUBSTR(TIMELEFT, 1, K-1);
TIMELEFT_T=INPUT(TRIM(TIMELEFT_2), TIME8.,);

IF L>0 THEN TIMELEFT_T=TIMELEFT_T+43200;
LEFT_DT=DHMS(TIMELEFT_2,0,0, TIMELEFT_T);

LABEL LEFT_DT = 'Date & Time arrived';
RUN;

*********************************************************************

PROC PRINT LABEL
DATA=MYSASLIB.CLEANED
(firstobs=1 obs=30);
var SUBJEcT ARRIVEDT TIMELEFT DATELEFT DATELEFT_2 TIMELEFT_T;
RUN;

 

So the book has the code up to STEP 5 - The others steps are irrelevant to my question so I cut 'em out - and when I execute it, it runs smoothly. The exercise asks me to replicate shown in STEP 5, which would be my STEP 6, but when I try to do it, it keeps bugging.

The log box displays:

 

4583 DATA=MYSASLIB.CLEANED
4584 (firstobs=1 obs=30);
4585 var SUBJEcT ARRIVEDT TIMELEFT DATELEFT DATELEFT_2 TIMELEFT_T;
ERROR: Variable DATELEFT_2 not found.
ERROR: Variable TIMELEFT_T not found.
4586 RUN;

 

And I don't understand why those variables aren't defined, if supposedly I'm just replicating STEP 5. There must be something that I'm overlooking.

 

 

Captura1.JPG

I attached the above image, so it's clearer what they meant to do in STEP 5 and of course what I'm trying to copy.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Go look for the FIRST ERROR in the log, not the last. You posted the log from the final proc print only, but your problems start with the FIRST statement you added, because that comes after a finished data step and is invalid, as it has to be in a data step and not in "open code".

 

What I mean by top-down:

  • start at the top of the log
  • scroll down till you find the first ERROR/WARNING
  • fix that
  • rerun the code
  • if there are still problems, repeat the process

Never assume that code you got from elsewhere will simply run. The code may me bogus as such, or fail because it comes from a different environment or makes assumptions.

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Community 🙂 Please show us the code you wrote. Not only what the log says

Franrua
Fluorite | Level 6

Thanks!

It is already there!  What I wrote is from STEP 6 (including) downward.

Kurt_Bremser
Super User

The last ERROR is always the least important. Debug your code top-down, because fixing the first problem might do away with all the others.

*****************************************************************STEP 5;

FORMAT ARRIVEDT DATETIME18.;
FORMAT DATEARRIVED2 DATE10. TIMEARRIVET TIME8.;
DATEARRIVED2=INPUT(TRIM(DATEARRIVED),MMDDYY10.);
I= FIND(TIMEARRIVE," ");
P=FIND(TIMEARRIVE,"P");
TIMEARRIVE2=SUBSTR(TIMEARRIVE,1,I-1);
TIMEARRIVET=INPUT(TRIM(TIMEARRIVE2),TIME8.);
IF P>0 THEN TIMEARRIVET=TIMEARRIVET+43200;
ARRIVEDT=DHMS(DATEARRIVED2,0,0,TIMEARRIVET) ;

Label ARRIVEDT="Date & Time Arrived";
RUN; /* This statement ends your data step and causes the following code to be invalid */

*****************************************************************STEP 6;
FORMAT LEFT_DT DATETIME18.;
FORMAT DATELEFT_2 DATE10. TIMELEFT_T TIME8.;
DATELEFT_2 = INPUT(TRIM(DATELEFT), MMDDYY10.);
Franrua
Fluorite | Level 6

MIster, I don't know if debugging from the top would help because, as I said, the code was already in the SAS file I opened. The part that I wrote, which is causing these bugs, starts in STEP 6.

I attach the whole code.

Kurt_Bremser
Super User

Go look for the FIRST ERROR in the log, not the last. You posted the log from the final proc print only, but your problems start with the FIRST statement you added, because that comes after a finished data step and is invalid, as it has to be in a data step and not in "open code".

 

What I mean by top-down:

  • start at the top of the log
  • scroll down till you find the first ERROR/WARNING
  • fix that
  • rerun the code
  • if there are still problems, repeat the process

Never assume that code you got from elsewhere will simply run. The code may me bogus as such, or fail because it comes from a different environment or makes assumptions.

Franrua
Fluorite | Level 6

Thanks, I think this kind of helped me to sort it out.

As suggested by you and other people, I misplaced the RUN statement in the DATA step, so I rearranged it as follows: 

 

FORMAT ARRIVEDT DATETIME18.;
FORMAT DATEARRIVED2 DATE10. TIMEARRIVET TIME8.;
DATEARRIVED2=INPUT(TRIM(DATEARRIVED),MMDDYY10.);
I= FIND(TIMEARRIVE," ");
P=FIND(TIMEARRIVE,"P");
TIMEARRIVE2=SUBSTR(TIMEARRIVE,1,I-1);
TIMEARRIVET=INPUT(TRIM(TIMEARRIVE2),TIME8.);
IF P>0 THEN TIMEARRIVET=TIMEARRIVET+43200;
ARRIVEDT=DHMS(DATEARRIVED2,0,0,TIMEARRIVET) ;

 

LABEL ARRIVEDT='Date & Time Arrived';

 

FORMAT LEFT_DT DATETIME18.;
FORMAT DATELEFT_2 DATE10. TIMELEFT_T TIME8.;
DATELEFT_2 = INPUT(TRIM(DATELEFT), MMDDYY10.);
K = FIND(TIMELEFT, ' ');
L= FIND(TIMELEFT, 'P');
TIMELEFT_2= SUBSTR(TIMELEFT, 1, K-1);
TIMELEFT_T=INPUT(TRIM(TIMELEFT_2), TIME8.);

IF L>0 THEN TIMELEFT_T=TIMELEFT_T+43200;
LEFT_DT=DHMS(DATELEFT_2,0,0, TIMELEFT_T);

 

LABEL LEFT_DT = 'Date & Time arrived';


RUN;

 

But the above worked ONLY after I closed the SAS session and reopened it.

I've got a vague idea of what it might've caused, but I'm unsure. The aforementioned book approached the QUIT and RUN statement and stated the following:

 

"If you fail to include a RUN statement at the end of your SAS job (or if SAS runs into an
error and never sees the RUN statement), the SAS processor may continue to run in the
background. This can cause unpredictable problems. If this occurs, press Ctrl-Break.
An option will appear allowing you to “Cancel Submitted Statements"

 

Similarly, knowing that when a SAS session is ended and all of the statements therein are therefore cancelled, lead me to believe that the error was somehow related with the RUN statement, since it was, to some extent, my most blatant mistake.

 

Do you really think it had to do anything with it or I'm completely lost?

 

P.D: sorry for my English.

Tom
Super User Tom
Super User

Missing a RUN statement is normally not that big a deal.  If you are running the code interactively and forget the RUN (or QUIT for the procs that need that instead) on the LAST procedure or data step then just that step won't until you finish defining it.

 

Most likely if it seems to not run any code you submit then you have given it unbalanced quotes (or possibly other things like parentheses or macro definitions) and it is just waiting for you to close them. 

 

You can sometimes recover from that state, but restarting is usually the surest way to get back to a stable state.

Astounding
PROC Star
2 errors ...

The code you added should become part of the existing long DATA step. So remove the RUN statement just before your additions.

Second, the long line of asterisks that you added needs a semicolon at the end of it. Otherwise it becomes a comment statement that comments out the PROC PRINT statement that follows.
Reeza
Super User

1. Format your code. Then it becomes quite easy to see where the issues may be.

 

DATA MYSASLIB.CLEANED;
	SET MYSASLIB.MESSYDATA;
	LABEL EDUCATION='Years of Schooling' HOW_ARRIVED='How Arrived at Clinic' 
		TOP_REASON='Top Reason for Coming' SATISFACTION='Satisfaction Score' 
		Subject="Subject ID" DateArrived="Date Arrived" TimeArrive="Time Arrived" 
		DateLeft="Date Left" TimeLeft="Time Left" Married="Married?" Single="Single?" 
		Age="Age Jan 1, 2014" Gender="Gender" Race="Race" 
		Satisfaction="Satisfaction Score";
	TEMP=ARRIVAL;
	DROP ARRIVAL;
	LABEL TEMP='Arrival Temperature';
	*****************************************************************STEP 5;
	FORMAT ARRIVEDT DATETIME18.;
	FORMAT DATEARRIVED2 DATE10. TIMEARRIVET TIME8.;
	DATEARRIVED2=INPUT(TRIM(DATEARRIVED), MMDDYY10.);
	I=FIND(TIMEARRIVE, " ");
	P=FIND(TIMEARRIVE, "P");
	TIMEARRIVE2=SUBSTR(TIMEARRIVE, 1, I-1);
	TIMEARRIVET=INPUT(TRIM(TIMEARRIVE2), TIME8.);

	IF P>0 THEN
		TIMEARRIVET=TIMEARRIVET+43200;
	ARRIVEDT=DHMS(DATEARRIVED2, 0, 0, TIMEARRIVET);
	Label ARRIVEDT="Date & Time Arrived"; *would recommend single quotes here otherwise the & may cause an issue;
RUN;

*****************************************************************STEP 6;
*WHICH DATA SET IS THIS APPLIED TO? THE run above terminated your data step, you either need
a new DATA and SET or you need to remove the previous RUN;
FORMAT LEFT_DT DATETIME18.; FORMAT DATELEFT_2 DATE10. TIMELEFT_T TIME8.; DATELEFT_2=INPUT(TRIM(DATELEFT), MMDDYY10.); K=FIND(TIMELEFT, ' '); L=FIND(TIMELEFT, 'P'); TIMELEFT_2=SUBSTR(TIMELEFT, 1, K-1); TIMELEFT_T=INPUT(TRIM(TIMELEFT_2), TIME8., ); IF L>0 THEN TIMELEFT_T=TIMELEFT_T+43200; LEFT_DT=DHMS(TIMELEFT_2, 0, 0, TIMELEFT_T); LABEL LEFT_DT='Date & Time arrived'; RUN; ********************************************************************* PROC PRINT LABEL DATA=MYSASLIB.CLEANED (firstobs=1 obs=30); var SUBJEcT ARRIVEDT TIMELEFT DATELEFT DATELEFT_2 TIMELEFT_T; RUN;

@Franrua wrote:

Hey, so I'm trying to learn SAS from this book called 'Master SAS for data analytics'.

 

I'm stuck with a proposed exercise (Chapter 6, fourth Hands-on example) exercise within this book and I'd appreciate if any of you could help me with it.  Here it goes:

 

DATA MYSASLIB.CLEANED;SET MYSASLIB.MESSYDATA;

LABEL
EDUCATION='Years of Schooling'
HOW_ARRIVED='How Arrived at Clinic'
TOP_REASON='Top Reason for Coming'
SATISFACTION='Satisfaction Score'
Subject="Subject ID"
DateArrived="Date Arrived"
TimeArrive="Time Arrived"
DateLeft="Date Left"
TimeLeft="Time Left"
Married="Married?"
Single="Single?"
Age="Age Jan 1, 2014"
Gender="Gender"
Race="Race"
Satisfaction="Satisfaction Score";
TEMP=ARRIVAL;
DROP ARRIVAL;
LABEL TEMP='Arrival Temperature';

*****************************************************************STEP 5;

FORMAT ARRIVEDT DATETIME18.;
FORMAT DATEARRIVED2 DATE10. TIMEARRIVET TIME8.;
DATEARRIVED2=INPUT(TRIM(DATEARRIVED),MMDDYY10.);
I= FIND(TIMEARRIVE," ");
P=FIND(TIMEARRIVE,"P");
TIMEARRIVE2=SUBSTR(TIMEARRIVE,1,I-1);
TIMEARRIVET=INPUT(TRIM(TIMEARRIVE2),TIME8.);
IF P>0 THEN TIMEARRIVET=TIMEARRIVET+43200;
ARRIVEDT=DHMS(DATEARRIVED2,0,0,TIMEARRIVET) ;

Label ARRIVEDT="Date & Time Arrived";
RUN;

*****************************************************************STEP 6;
FORMAT LEFT_DT DATETIME18.;
FORMAT DATELEFT_2 DATE10. TIMELEFT_T TIME8.;
DATELEFT_2 = INPUT(TRIM(DATELEFT), MMDDYY10.);
K = FIND(TIMELEFT, ' ');
L= FIND(TIMELEFT, 'P');

TIMELEFT_2= SUBSTR(TIMELEFT, 1, K-1);
TIMELEFT_T=INPUT(TRIM(TIMELEFT_2), TIME8.,);

IF L>0 THEN TIMELEFT_T=TIMELEFT_T+43200;
LEFT_DT=DHMS(TIMELEFT_2,0,0, TIMELEFT_T);

LABEL LEFT_DT = 'Date & Time arrived';
RUN;

*********************************************************************

PROC PRINT LABEL
DATA=MYSASLIB.CLEANED
(firstobs=1 obs=30);
var SUBJEcT ARRIVEDT TIMELEFT DATELEFT DATELEFT_2 TIMELEFT_T;
RUN;

 

So the book has the code up to STEP 5 - The others steps are irrelevant to my question so I cut 'em out - and when I execute it, it runs smoothly. The exercise asks me to replicate shown in STEP 5, which would be my STEP 6, but when I try to do it, it keeps bugging.

The log box displays:

 

4583 DATA=MYSASLIB.CLEANED
4584 (firstobs=1 obs=30);
4585 var SUBJEcT ARRIVEDT TIMELEFT DATELEFT DATELEFT_2 TIMELEFT_T;
ERROR: Variable DATELEFT_2 not found.
ERROR: Variable TIMELEFT_T not found.
4586 RUN;

 

And I don't understand why those variables aren't defined, if supposedly I'm just replicating STEP 5. There must be something that I'm overlooking.

 

 

Captura1.JPG

I attached the above image, so it's clearer what they meant to do in STEP 5 and of course what I'm trying to copy.

 

Thank you!


 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 2459 views
  • 3 likes
  • 6 in conversation