BookmarkSubscribeRSS Feed
Rahul_SAS
Quartz | Level 8

Hi Experts,

 

Please help me to convert the following query in sas query.

 

I tried but getting errors.

 

SELECT Employee_Name, ProjectCode, QCErrorLevelName,

CAST(SUM(Perc)*100 as VARCHAR)+'%' Error_Rate FROM

(

select CAST(COUNT(DISTINCT SafetyCaseID) as FLOAT)/

CAST((select COUNT(Distinct SafetyCaseID)

from dbo.VWCDSCPQualityMetricsRpt P1

where P1.ProjectCode = P.ProjectCode and P1.Employee_Name = P.Employee_Name

GROUP BY ProjectCode,Employee_Name) as FLOAT) as Perc

,ProjectCode,Employee_Name, (CASE WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName ELSE 'No Error' End) QCErrorLevelName

from dbo.VWCDSCPQualityMetricsRpt P

where QCErrorLevelName LIKE 'L%'

GROUP BY ProjectCode,Employee_Name,(CASE WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName ELSE 'No Error' End)

) A

GROUP BY ProjectCode, QCErrorLevelName,Employee_Name

6 REPLIES 6
SASKiwi
PROC Star

The easiest solution is SQL passthrough - you will need to add your own database connection options:

 

proc sql;
  connect to odbc (** put your connection options here **);
  create table want as 
  select  *
  from connection to odbc
( 
 SELECT Employee_Name, ProjectCode, QCErrorLevelName,
CAST(SUM(Perc)*100 as VARCHAR)+'%' Error_Rate FROM
(
select CAST(COUNT(DISTINCT SafetyCaseID) as FLOAT)/
CAST((select COUNT(Distinct SafetyCaseID)
from dbo.VWCDSCPQualityMetricsRpt P1
where P1.ProjectCode = P.ProjectCode and P1.Employee_Name = P.Employee_Name
GROUP BY ProjectCode,Employee_Name) as FLOAT) as Perc
,ProjectCode,Employee_Name, (CASE WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName ELSE 'No Error' End) QCErrorLevelName
from dbo.VWCDSCPQualityMetricsRpt P
where QCErrorLevelName LIKE 'L%'
GROUP BY ProjectCode,Employee_Name,(CASE WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName ELSE 'No Error' End)
) A
GROUP BY ProjectCode, QCErrorLevelName,Employee_Name

);
quit;
Swagat711
Fluorite | Level 6
You can use pass through for running the query as it is in sas.

You will get the Path and Authdomain from Library details or libname details for pass through query
Kurt_Bremser
Super User

Start by giving this awful eyesore of spaghetti code some visual formatting, so you can see functional blocks.

Replace the CAST functions with put where needed (eg the cast to float is not needed, as all numbers in SAS are real numbers).

s_lassen
Meteorite | Level 14

I think it is mostly getting rid of the CAST calls, as they are not in SAS SQL. The CASTs to FLOAT are unnecessary, as SAS numbers are floats anyway. The CAST to a string is not necessary, just put a format on the number to display it as a percent:

SELECT 
  Employee_Name, 
  ProjectCode, 
  QCErrorLevelName,
  SUM(Perc) format=percent7.2 as Error_Rate 
FROM
  (select 
     COUNT(DISTINCT SafetyCaseID)/
              (select COUNT(Distinct SafetyCaseID) from dbo.VWCDSCPQualityMetricsRpt P1
               where P1.ProjectCode = P.ProjectCode and P1.Employee_Name = P.Employee_Name
               GROUP BY ProjectCode,Employee_Name) as Perc,
     ProjectCode,
     Employee_Name, 
     CASE 
       WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName 
       ELSE 'No Error' 
     End as QCErrorLevelName
   from dbo.VWCDSCPQualityMetricsRpt P
   where QCErrorLevelName LIKE 'L%'
   GROUP BY ProjectCode,Employee_Name,(CASE WHEN QCErrorLevelName LIKE 'L%' Then QCErrorLevelName ELSE 'No Error' End)
   ) A
GROUP BY ProjectCode, QCErrorLevelName,Employee_Name;
Rahul_SAS
Quartz | Level 8

Rahul_SAS
Quartz | Level 8

 

 

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
  • 6 replies
  • 891 views
  • 0 likes
  • 5 in conversation