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

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1880 views
  • 0 likes
  • 5 in conversation