BookmarkSubscribeRSS Feed
Arvind1
Calcite | Level 5

Dear all, 

 

I have a table where in I have to replace value in two columns  ( Customer_Complaint and Status) based on the value present in them. I tried using SQL with CASE statement to modify the variables. However i am able to update values only in one particular column (Customer_Complaint) and not in other Status column.

 

Is there a way to update data in both columns ? I tried the below code, however i am getting an error when using multiple case statements.

 

Table structure : Ticket /Customer_Complaint/ Status

 

proc sql;
select * ,
case
when customer_complaint like '%Speed%' then 'Speed'
when customer_complaint like '%Service%' then 'Service'
when customer_complaint like '%Data%' then 'Data'
else 'Miscellaneous'
end as Customer_Complaint1

Case 

when status like 'Pending' then 'Closed'

end as Status_1
from work.comcast;

quit;

 

Is there a way that i first update the data in the first column (Customer_Complaint), write into a temporary file and then perform operation on the temporary file to update the Status column?

 

or is there a way to update file using replace? Please guide

thanks in advance for your help.

 

 

8 REPLIES 8
Krueger
Pyrite | Level 9

You are missing a comma after Customer_Complaint1 which I added in. This would cause only 1 row to appear assuming it would run successfully.

 

You might also want to add an ELSE to your CASE for Status_1.

 

proc sql;
select * ,
case
when customer_complaint like '%Speed%' then 'Speed'
when customer_complaint like '%Service%' then 'Service'
when customer_complaint like '%Data%' then 'Data'
else 'Miscellaneous'
end as Customer_Complaint1,

Case 

when status like 'Pending' then 'Closed'

end as Status_1
from work.comcast;

quit;

 

Arvind1
Calcite | Level 5

Thank you Kruger. That helped a lot . Am able to update the values in both the columns.

 

However one more doubt, is there any other alternate way to update the values in the columns when importing or reading the file using replace ?

 

 

 

 

Tom
Super User Tom
Super User

How are you reading the data.  I should be simple to write DATA STEP code to do what ever you want. 

 

If you are using a data step to create the original file then just add the logic to that step.

data comcast;
  ... whatever logic you currently have that creates CUSTOMER_COMPLAINT and STATUS ...
  Customer_Complaint1= 'Miscellaneous';
  select ;
    when (index(customer_complaint,'Speed')) Customer_Complaint1='Speed';
    when (index(customer_complaint,'Service')) Customer_Complaint1='Service';
    when (index(customer_complaint,'Data')) Customer_Complaint1='Data';
    otherwise ;
  end;
  if status='Pending' then Status_1='Closed';
run;
PaigeMiller
Diamond | Level 26

@Arvind1 wrote:

Dear all, 

 

I have a table where in I have to replace value in two columns  ( Customer_Complaint and Status) based on the value present in them. I tried using SQL with CASE statement to modify the variables. However i am able to update values only in one particular column (Customer_Complaint) and not in other Status column.

 

Is there a way to update data in both columns ? I tried the below code, however i am getting an error when using multiple case statements.

 

 


What error?

 

Show us the log (the code in the log as well as the error messages in the log)

--
Paige Miller
Arvind1
Calcite | Level 5

Thanks Paige . The solution has been arrived. However would like to know if there is an alternate way of replacing the data in the file other than the sql method. 

Reeza
Super User
I'd recommend a data step and listing the words in a temporary array and then looping through the temporary array. That way if your word list gets longer you're only updating the list once.

What happens if a statement has more than one of the keywords?
Arvind1
Calcite | Level 5

Thanks everyone for your responses.

 

To give a background, i am doing a data clean up of the columns - Customer complaint and Status. When there is a word speed in the customer complaint i would want to mark it as speed issues and when there is poor customer service i would want to mark it as service issue etc.  rest of the complaints would be miscellaneous.

 

Other than the SQL method is there anyother way that i can replace the data in the file. Attaching the file for your reference and the working SQL query. 

 

FILENAME REFFILE "/home/u44638328/Comcast.csv";

proc import datafile=REFFILE
DBMS=CSV
OUT=work.Comcast replace;
getnames=YES;
run;

proc sql;
select * ,
case
when customer_complaint like '%Speed%' then 'Speed'
when customer_complaint like '%speed%' then 'Speed'
when customer_complaint like '%Service%' then 'Service'
when customer_complaint like '%service%' then 'Service'
when customer_complaint like '%Data%' then 'Data'
when customer_complaint like '%data%' then 'Data'
when customer_complaint like '%billing%' then 'Billing'
when customer_complaint like '%Billing%' then 'Billing'
when customer_complaint like '%Price%' then 'Pric'
when customer_complaint like '%Outage%' then 'Outage'
when customer_complaint like '%outage%' then 'Outage'
else 'Miscellaneous'
end as Customer_Complaint1,

Case
when status like '%end%' then 'Open'
when status like '%ved%' then 'Closed'
when status like 'Closed' then 'Closed'
when status like 'Open' then 'Open'

end as Status_1
from work.comcast;

quit;

 

 

Krueger
Pyrite | Level 9

I'm new to SAS so unsure if this is going to run as expected but here's doing it as a datastep. You can use upcase() or lowcase() to remove some of your duplicate code.

 

data want;
	set work.comcast;
	if upcase(customer_complaint) = 'SERVICE' then Customer_Complaint1='Service';
	else if upcase(customer_complaint) = 'DATA' then Customer_Complaint1='Data';
	else if upcase(customer_complaint) = 'BILLING' then Customer_Complaint1='Billing';
	else if upcase(customer_complaint) = 'PRICE' then Customer_Complaint1='Price';
	else if upcase(customer_complaint) = 'OUTAGE' then Customer_Complaint1='Outage';
	else Customer_Complaint1='Miscellaneous';

	if upcase(status) = 'END' then Status_1 = 'Open';
	else if upcase(status) = 'VED' then Status_1 = 'Closed';
	else if upcase(status) = 'CLOSED' then Status_1 = 'Closed';
	else if upcase(status) = 'OPEN' then Status_1 = 'Open';
run;

Some example data would also be helpful. I'm assuming the above isn't going to fully work as it appears your searching a string of text. In that case you will likely need to experiment with the find() function in SAS.

 

Edit: Instead of find() you might have to do =: in place of like. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7509 views
  • 1 like
  • 5 in conversation