BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

I have the data set test where I need to have the 24 hour time point for dv to be less than the 20 hour time point for dv based upon the calculation dv=ipred(1+eps1) +eps2 which is possible since the eps1 and eps2 are based upon a random function..  In those cases such as for subjects 2,3,and 4 I would like to have the dv at time 24 to be less than the dv time at 20 hrs.  Thus I want it to recalculate until dv<lag(dv)  then exit.  However I can't determine if the logic of my code works  since I can't figure out how to code for less than in the do until loop and I get a syntax error  stating "expecting one of the following (, =.)."

I need to know how to correct my code or is there another way to accomplish this task perhaps with proc seql?

data test;
input Subject Time dv ipred eps1 eps2;
datalines;
1 10 10 8  0.01 0.1
1  12 5 3  0.01 0.1
1 20 10 8  0.01 0.1
1 24 5  3  0.01 0.1
2 10 40 38 0.01 0.1
2  12 20 18 0.01 0.1
2 20 10 8  0.01 0.1
2 24 15 14 0.01 0.1
3 10 5 3  0.01 0.1
3  12 21   0.01 0.1
3 20 20 19 0.01 0.1
3 24 30 32 0.01 0.1
4 10 5 3   0.01 0.1
4  12 21 1 0.01 0.1
4 20 20 15 0.01 0.1
4 24 30 25 0.01 0.1

;
run;



data old;
set test;
retain subject dv lagdv dv  eps1 eps2;
if time in (20 24);
lagdv=lag(dv);
%let eps1=0.0025;
%let eps2=0.096;
eps1=rand('LOGNORMAL')*sqrt(&eps1);
eps2=ranD('LOGNORMAL')*sqrt(&eps2);
dv=ipred*(1+eps1) + eps2;
array subs subject;
do i=1 to dim(subs);
if dv>lag(dv) then do until dv<lag(dv);
output;
keep subject time lagdv dv  eps1 eps2;
end;
end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star
The UNTIL clause (and the WHILE clause) in a DO statement must have its argument in parentheses. So "do until dv;", if logically appropriate, should be "do until (dv);".
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

So, first you have an error in the log. Show us the entire log for this step, the code AND the error message, by clicking on the {i} icon and paste the entire log for this step (not just the error message) into the window that appears. Do not show us the log any other way.

 

Can you take one example, and work through the results by hand, and then show us the desired output? Even better yet, in this example, show us the hand-calculations so we can see exactly what you want to do. It is unnerving to hear someone say they can't even determine if they are getting the right results, how would we know what the right results are to help you, if you don't tell us the right results?

 

Lastly, you have made this code unnecessarily complicated so its impossible to determine what you really are trying to do. You might try starting over and trying to simplify as much as possible ... by removing the macro variables which are completely unnecessary, removing the ARRAY of length 1, which is also completely unnecessary, and I also suspect you don't need the RETAIN statement.

--
Paige Miller
jacksonan123
Lapis Lazuli | Level 10
The result that I would like to have and I will use only subject #4 as an
example but it would be done for all subjects.

Original data

Subject Time DV IPRED eps1 eps2

4 10 5 3 0.01 0.1

4 12 21 1 0.01 0.1

4 20 20 15 0.01 0.1

4 24 30 25 0.01 0.1

Recalculation of values at 20 and 24 hrs which will result in a number at 24
hrs less than the value at 20 hrs

4 10 5 3 0.01
0.1

4 12 21 1 0.01
0.1

4 20 (new value) 15 0.01 0.1

4 24 (value lt 20) 25 0.01 0.1





The log is:

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

72

73

74

75

76 data test;

77 input Subject Time dv ipred eps1 eps2;

78 datalines;

NOTE: SAS went to a new line when INPUT statement reached past the end of a
line.

NOTE: The data set WORK.TEST has 15 observations and 6 variables.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.02 seconds





96 ;

97 run;

98

99

100

101 data old;

102 set test;

103 retain subject dv lagdv dv eps1 eps2;

104 if time in (20 24);

105 lagdv=lag(dv);

106 %let eps1=0.0025;

107 %let eps2=0.096;

108 eps1=rand('LOGNORMAL')*sqrt(&eps1);

109 eps2=ranD('LOGNORMAL')*sqrt(&eps2);

110 dv=ipred*(1+eps1) + eps2;

111 array subs subject;

112 do i=1 to dim(subs);

113 if dv>lag(dv) then do until dv
__

22

76

ERROR 22-322: Syntax error, expecting one of the following: (, =.



ERROR 76-322: Syntax error, statement will be ignored.

114 output;

115 keep subject time lagdv dv eps1 eps2;

116 end;

117 end;

118 run;

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.OLD may be incomplete. When this step was
stopped there were 0 observations and 6 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds





119




mkeintz
PROC Star
The UNTIL clause (and the WHILE clause) in a DO statement must have its argument in parentheses. So "do until dv;", if logically appropriate, should be "do until (dv);".
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

And here is a reason that @PaigeMiller requested that you use the {I} or "running man" opened code box:

113 if dv>lag(dv) then do until dv
__
22
76
ERROR 22-322: Syntax error, expecting one of the following: (, =.
ERROR 76-322: Syntax error, statement will be ignored.

Besides all of the extra blank lines, if you had copied directly from your log and pasted into a code box the _ (underscore character) above would have been under the place in the line where SAS determined a problem existed. Likely something more like:

113 if dv>lag(dv) then do until dv 
                                _ 
                               22 
                               76 
ERROR 22-322: Syntax error, expecting one of the following: (, =. 
ERROR 76-322: Syntax error, statement will be ignored. 

which shows that a "(" was expected before the variable DV in the UNTIL,  as @mkeintz solution indicated.

 

The main message windows on this forum will reformat text when pasted, removing white space such as leading blanks. The code box will maintain plain text formatting.

 

jacksonan123
Lapis Lazuli | Level 10
Yes, that was the issue and thanks for your feedback.

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
  • 5 replies
  • 693 views
  • 2 likes
  • 4 in conversation