Desktop productivity for business analysts and programmers

Modifying a value in one data set based on another data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Modifying a value in one data set based on another data set

I am trying to modify a value in a field in one data set based on criteria for this and another data set.  Here is my current code, which doesn't work.  My code should change the hazard code from MO to WM in table B where the claim number is the same between the two tables and there are values in the first table for both loss causes WM and WI. 

proc sql;

UPDATE TABLE_B AS B

HAZARD_CODE_INDICATOR = 'WM'

FROM TABLE_A AS A

where

A.CLM_NBR = B.CLM_NBR AND

A.LOSS_CAUSE = 'MO' AND

(B.HAZARD_CODE_INDICATOR = 'WM' AND

B.VALUES <> 0) AND

(B.HAZARD_CODE_INDICATOR = 'WI' AND

B.VALUES <> 0));

quit;

 

Here are the error messages I receive:

 

23 proc sql;

24 UPDATE TABLE_B AS B

25 HAZARD_CODE_INDICATOR = 'WM'

_____________________

79

26 FROM FROM TABLE_A AS A

____ __

22 76

202

ERROR 79-322: Expecting a SET.

ERROR 22-322: Syntax error, expecting one of the following: ;, !!, *, **, +, ',', -, /, WHERE, ||.

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

ERROR 202-322: The option or parameter is not recognized and will be ignored.

26 ! FROM FROM TABLE_A AS A

__

22

ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN,

CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

 

 

Thank you for any help you can provide,

Christine


Accepted Solutions
Solution
‎07-31-2017 03:10 PM
Respected Advisor
Posts: 4,173

Re: Modifying a value in one data set based on another data set

[ Edited ]

@Seabird

The following should work.

 

data table_a;
  CLM_NBR=1;
  LOSS_CAUSE = 'MO';
  HAZARD_CODE_INDICATOR='WM';
  output;
  stop;
run;

data table_b;
  CLM_NBR=1;
  HAZARD_CODE_INDICATOR = 'WI';
  VALUES=1;
  output;
  CLM_NBR=1;
  HAZARD_CODE_INDICATOR = 'XX';
  VALUES=1;
  output;
  stop;
run;

proc sql;
  UPDATE TABLE_B AS B
    set HAZARD_CODE_INDICATOR = 'WM'
  where 
    B.HAZARD_CODE_INDICATOR = 'WI' AND
    B.VALUES <> 0 AND
    exists
  	(
  		select *
  		from table_a as a
  	    where
  	      A.CLM_NBR = B.CLM_NBR AND
  	      A.LOSS_CAUSE = 'MO' AND
  	      A.HAZARD_CODE_INDICATOR = 'WM'
  	)	
	;
quit;

 

 

View solution in original post


All Replies
Super User
Posts: 19,822

Re: Modifying a value in one data set based on another data set

I don't think SAS SQL supports updates with a join using SQL.

 

Try creating a new table using SQL rather than updating the table.

Solution
‎07-31-2017 03:10 PM
Respected Advisor
Posts: 4,173

Re: Modifying a value in one data set based on another data set

[ Edited ]

@Seabird

The following should work.

 

data table_a;
  CLM_NBR=1;
  LOSS_CAUSE = 'MO';
  HAZARD_CODE_INDICATOR='WM';
  output;
  stop;
run;

data table_b;
  CLM_NBR=1;
  HAZARD_CODE_INDICATOR = 'WI';
  VALUES=1;
  output;
  CLM_NBR=1;
  HAZARD_CODE_INDICATOR = 'XX';
  VALUES=1;
  output;
  stop;
run;

proc sql;
  UPDATE TABLE_B AS B
    set HAZARD_CODE_INDICATOR = 'WM'
  where 
    B.HAZARD_CODE_INDICATOR = 'WI' AND
    B.VALUES <> 0 AND
    exists
  	(
  		select *
  		from table_a as a
  	    where
  	      A.CLM_NBR = B.CLM_NBR AND
  	      A.LOSS_CAUSE = 'MO' AND
  	      A.HAZARD_CODE_INDICATOR = 'WM'
  	)	
	;
quit;

 

 

Occasional Contributor
Posts: 5

Re: Modifying a value in one data set based on another data set

Thank you very much, Patrick!  You saved my bacon!  I've been struggling with this for a while and was nearing a deadline.

 

Seabird

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 247 views
  • 2 likes
  • 3 in conversation