IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SAS: Update query


IBM Mainframe Forums -> All Other Mainframe Topics
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 7:14 pm
Reply with quote

Hi ,

I have a below requirement

Requirement:-
I have 2 tables, say table A and table B. I have to update a column in table A with a column in table B based on the matching criteria. If the matching criteria fails, then I have to retain the value that is already present. Else I have to update the table A with the value present in table B

...
Below is the code that I tried
Code:

UPDATE STUDENT_TAB A
  SET  GRADE = (SELECT CASE
                          WHEN B.GRADE IS MISSING
                               THEN A.GRADE
                               ELSE B.GRADE
                       END
                   FROM GRADE_TAB B
                  WHERE B.STUD_NO = A.STUD_NO)


Input data:
Code:

STUDENT_TAB
-----------------
STUD_NO  GRADE
111           A
222           B
333                    ==> SPACES IS ALSO VALID
444           D


GRADE_TAB
--------------
STUD_NO  GRADE
111           X
222           B
555           Z


Expected result:-
------------------
Code:

STUDENT_TAB
STUD_NO  GRADE
111           X
222           B
333                     ==> SPACES IS ALSO VALID
444           D


but receiving the following result
Code:

STUDENT_TAB
STUD_NO  GRADE
111           X
222           B
333                    ==> SPACES IS ALSO VALID
444     


I want to implement this query in SAS. I am unable to handle the missing/null values returned in the sub-query... can anyone assist me...??

TIA....
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Thu Jan 21, 2010 7:27 pm
Reply with quote

COALESCE (column-name <, ... column-name>) could probably help you with that.

COALESCE accepts one or more column names of the same data type. The COALESCE function checks the value of each column in the order in which they are listed and returns the first nonmissing value. If only one column is listed, the COALESCE function returns the value of that column. If all the values of all arguments are missing, the COALESCE function returns a missing value.
Back to top
View user's profile Send private message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 7:43 pm
Reply with quote

I have modified my query as I assume that my above query will not work properly...

Code:

PROC SQL;
UPDATE STUDENT_TAB A
  SET  GRADE =  CASE WHEN (SELECT B.GRADE  INTO :BGRADE
                             FROM GRADE_TAB B
                            WHERE B.STUD_NO = A.STUD_NO)
                            IS MISSING
                               THEN A.GRADE
                               ELSE "&BGRADE"
                       END
;
QUIT;


And I am receiving the following warning message in SAS log
Code:

WARNING: INTO clause that is not in the outermost query block will be ignored. 
WARNING: Character expression will be truncated when assigned to character column GRADE.


looks like the macro variable &BGRADE is not resolved properly.... Any clue where it went wrong...
Back to top
View user's profile Send private message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 7:52 pm
Reply with quote

Just came to know the below info...


Quote:

Use the INTO clause only in the outer query of a SELECT statement and not in a subquery.


Any idea on using the returned value(B.GRADE) for UPDATE...??
Back to top
View user's profile Send private message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 8:30 pm
Reply with quote

Thank You Peter, looks like COALESCE has worked... intial results are OK. I have to verify by loading some huge no.of records... thanks alot...
Back to top
View user's profile Send private message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 8:38 pm
Reply with quote

Also, can you assist me in writing multiple column update as below in SAS..

Code:

UPDATE TABLE_A A
 SET (COL1,COL2,COL3) = SELECT B.COL1,B.COL2,B.COL3 FROM TABLE_B B
;


I am receiving syntax errors when written in above format.. is this way of writing the query allowed in SAS/PROC SQL ..??
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8697
Location: Dubuque, Iowa, USA

PostPosted: Thu Jan 21, 2010 8:54 pm
Reply with quote

Quote:
is this way of writing the query allowed in SAS/PROC SQL ..??
What does the manual tell you? SAS has their documentation online, and they have a PROC SQL manual.
Back to top
View user's profile Send private message
rajesh1183

New User


Joined: 07 Jan 2008
Posts: 98
Location: Hyderabad

PostPosted: Thu Jan 21, 2010 9:09 pm
Reply with quote

from the syntax, I think, the above requested query is not possible

Code:

UPDATE table-name|sas/access-view|proc-sql-view <AS alias>  SET column=sql-expression
<, ... column=sql-expression> 
 <SET column=sql-expression
<, ... column=sql-expression>> 
 <WHERE sql-expression>; 
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> All Other Mainframe Topics

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top