BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have read that sql is a declarative language.

Can anyone send an example why  sql is a declarative language?

Is SAS also a declarative language?

Can anyone explain IF  SAS is a procedural language?

 

Thanks

 

2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
This is a fun article on imperative vs declarative languages: https://tylermcginnis.com/imperative-vs-declarative-programming/ and this does a decent job at procedural languages: https://en.wikipedia.org/wiki/Procedural_programming . And this is also good: https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms .
Hope this helps,
cynthia
s_lassen
Meteorite | Level 14

SQL is considered declarative, because you tell the computer what you want, not what to do. 

 

E.g.:

create table x as select * from sashelp.class where sex='F' order by age,weight;

A very simple example, but basically, you do not tell the computer whether it should extract the data and sort afterwards, or sort first and then extract the records. I assume it does the first, but with more complicated examples there can be many ways to do things.

 

By SAS I suppose you mean SAS datastep language and base SAS procedures (excluding SQL).

 

The same thing as above can be accomplished with SORT and datastep in more than one way:

data x;
  set sashelp class;
  if sex='F';
run;

proc sort;
  by age weight;
run;

or

proc sort data=sashelp.class out=temp;
  by age weight;
run;

data x;
  set temp;
  if sex='F';
run;

The first method extracts first, and then sorts. The other does it the other way round.

 

You could also just use

 

proc sort data=sashelp.class out=x;
  where sex='F';
  by age weight;
run;

 

 which does exactly what I expect SQL to implement in this simple example, but here the instructions are implicit:

  1. Find those records that match the criteria
  2. Sort them and output

 

Note that it is the use of the WHERE statement which defines that things are executed this way round, not the fact that the WHERE statement comes before the BY statement.

 

You can also do it the other way round with just PROC SORT:

 

proc sort data=sashelp.class out=x(where=(sex='F'));
  by age weight;
run;

Here, the WHERE clause is on the output, meaning that we tell SAS to sort first and select afterwards.

 

 

The point is that in all these cases, we defined very clearly (although you have to have some experience to tell the difference) what was going to happen, and in what order. That's procedural. With SQL, we basically told what we wanted, and let the SQL interpreter decide how to do it. That's declarative.

 

Of course, there are degrees of declarativeness, especially when it comes to the procedures. With PROC SORT, we could have used options to tell SAS what kind of algorithm to use (e.g. TAGSORT, or invoking SYNCSORT, if we had that on our installation), but we chose not to, meaning that SAS will default to standard SAS sort (which may be changed or upgraded once in a rare while, or perhaps our installation has SYNCSORT set as default, meaning that that would be used).

 

 

 

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
  • 2 replies
  • 1152 views
  • 0 likes
  • 3 in conversation