BookmarkSubscribeRSS Feed
anlynch
Calcite | Level 5

Hi,

I have written some code to update a table with various values from another table, however, SAS seems to randomly skip updating rows, even when the conditions at satisfied. I have noticed that if I restart SAS (close and re-open) the problem seems to be alleviated to some degree, but there will still missing values at times. I would say that this happens about 90% of the time I try and run the code. Furthermore, each time I run the code, (even with the same input data) the rows that don't get updated are different each time.

%if &i=1 %then %do;
proc sql feedback;
alter table l.raw_%sysfunc(date(),date7.)
add A num format best32.
add B num format best32.;
update l.raw_%sysfunc(date(),date7.) t1
set A = (select ppg_total_&rpi2._a from l.raw_%sysfunc(date(),date7.)_copy where monotonic() = &i) where monotonic() = &i;
update l.raw_%sysfunc(date(),date7.) t1
set B = (select ppg_allow_total_&rpi2._a from l.raw_%sysfunc(date(),date7.)_copy where monotonic() = &i) where monotonic() = &i;
quit;
%end;
%else %do;
proc sql;
update l.raw_%sysfunc(date(),date7.) t1
set A = (select ppg_total_&rpi2._a from l.raw_%sysfunc(date(),date7.)_copy where monotonic() = &i) where monotonic() = &i;
update l.raw_%sysfunc(date(),date7.) t1
set B = (select ppg_allow_total_&rpi2._a from l.raw_%sysfunc(date(),date7.)_copy where monotonic() = &i) where monotonic() = &i;
quit;
%end;
8 REPLIES 8
Astounding
PROC Star

I can't tell if this is the problem or not, but I believe that SAS considers the MONOTONIC function to be an undocumented, unsupported feature.  It could be a source of problems.

anlynch
Calcite | Level 5

yea, i had a feeling that monotonic might have something to do with it. i am trying to think of a solution to work around using monotonic.

Reeza
Super User

It seems a bit unintuitive in SAS to update row by row like that as well. I've seen that done using cursors in SQL, but not in SAS.

It looks like straight join by row, and that A and B are set to the same thing?  I'm not sure if you simplified the code to post it.

anlynch
Calcite | Level 5

A and B are different variables, but the way in which they are retrieved are the same (one is pgg_total and one is ppg_allow).

the problem i am running into is getting sas to update only one row at a time... which is why i have two monotonic() constraints. the first retrieves the value from the table, and then the second only updates one row at a time.

edit: i am guessing i can figure out a better way to write this, without using monotonic() like this, but its intriguing to me that SAS evaluates it differently each time the code is executed.

SASKiwi
PROC Star

Why not write your updating process in a DATA step where you have full row-by-row control and don't require undocumented features?

anlynch
Calcite | Level 5

I tend to use sql as a crutch... more than I should.

Anyhow, I still kept the meat of the sql there, but instead of using monotonic i used a data step to create a macro variable to keep the constraints.

...and before I wrote this post, I didn't realize that monotonic was unsupported.

SASKiwi
PROC Star

Understandible if you come from an SQL background, but SAS DATA step processing goes way beyond what you can do in SQL with the bonus of row-by-row control.

I've done SQL stored procedures with cursors to get row control but its way more complicated than DATA steps Smiley Wink.

Tom
Super User Tom
Super User

Can you explain in non code what that program is trying to do?

I wouldn't normally use an "ALTER TABLE" statement in SAS.  To add a column to a table would require SAS to totally recreate the table. So at that point why not just use a data step or an SQL create table statement to make the table you want?

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
  • 8 replies
  • 919 views
  • 3 likes
  • 5 in conversation