BookmarkSubscribeRSS Feed
Reggiete
Calcite | Level 5

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

6 REPLIES 6
Reeza
Super User

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?

 

Reggiete
Calcite | Level 5

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

Reeza
Super User

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;
Reggiete
Calcite | Level 5

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

Reeza
Super User

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

novinosrin
Tourmaline | Level 20

@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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 920 views
  • 0 likes
  • 3 in conversation