DATA Step, Macro, Functions and more

SAS Proc SQL Help

Reply
Occasional Contributor
Posts: 8

SAS Proc SQL Help

Hello, 

 

I am new to SAS. I am trying to write the below SQL script in SAS and wrap in Proc SQL 

 

FinalStatus = CAST(SUBSTRING(MAX(CAST(cl.DateNotesEntered AS BINARY(6)) + CAST(cl.CallResult AS VARBINARY(1000))), 7, 1000) AS VARCHAR(1000)),

 

Any help on how this statement would be written in Proc SQL for SAS

Super User
Posts: 24,012

Re: SAS Proc SQL Help

CAST isn't a SAS function, you would use PUT instead. 

What type of SQL is that? 
I though the variable name was always at the end?

 

This may be easier if you show what you have and what you need, there may be other ways in SAS that are more efficient?

 

Occasional Contributor
Posts: 8

Re: SAS Proc SQL Help

yes you are right the variable name is normally at the end but you can also put it in the beginning. the SQL code above was writen in SQL server. 

 

Basically what the line of sql is giving the max status for the max date

 

so for example, below i would want to return the max status which is the record with the greatest date. 

 

Max status would be Completed. 

 

A                  Date                   Status

Example      1/1/2017            Open

Example       1/3/2017             Pending

Example        1/4/2017            Completed

Super User
Posts: 24,012

Re: SAS Proc SQL Help

Do you have to use SQL?

A Data Step does this very easily.

 

proc sort data=have;
by a date;
run;

data want;
set have;
by a date;
if last.a; *takes the last record of each A group;
run;
Occasional Contributor
Posts: 8

Re: SAS Proc SQL Help

Yes i would prefer sql since i have a entire statement i am running to create a table. 

Super User
Posts: 24,012

Re: SAS Proc SQL Help

I think you'll have to do a subquery and join then, I don't think this will translate into SAS code directly. 

Super User
Posts: 2,061

Re: SAS Proc SQL Help

@Reggiete  I'm not sure if I understood your req as I am wondering what is the big deal in proc sql. Please let me know if i'm missing something:

 

data have;

input A   : $10.              Date   :mmddyy10.                Status $; /*assuming the date is mmddyy10.*/

format date mmddyy10.;

datalines;

Example      1/1/2017            Open

Example       1/3/2017             Pending

Example        1/4/2017            Completed

;

 

 

proc sql;

create table want as

select *

from have

group by a

having date=max(date);

quit;

Ask a Question
Discussion stats
  • 6 replies
  • 141 views
  • 0 likes
  • 3 in conversation