BookmarkSubscribeRSS Feed

Programming with CASL in SAS Viya for Learners – first steps

Started ‎08-15-2022 by
Modified ‎08-12-2022 by
Views 1,652

I often used to joke with younger members of one of the programming teams I worked with that “real programmers pexels-markus-spiske-360591.jpg don’t use GUIs”. I was only half joking though as while I use GUIs (graphical user interfaces) for programming and I’ve been a big fan of SAS Enterprise Guide and SAS Viya’s visual programming products I still believe that in order to be a really good professional programmer nothing can beat actually learning to hand craft the code yourself rather than relying too much on code generators.

 

Therefore in this series of articles I shall be demonstrating how to use CASL (Cloud Analytical Services Language) in SAS Viya for Learners using SAS Studio. We will start by seeing how to initialize a CAS session, examine the many sample data sets provided, run a simple statistical process and terminate the session.

 

What is CASL?

 

CASL is the scripting language used to execute CAS actions in a CAS server. CAS actions perform tasks and are grouped together in action sets based on common functionality.

 

Starting a session

 

Start SAS Viya for Learners and open SAS Studio from the menu.

 

ScShot1.png

 

Create a new SAS program and start a CAS session. This can very simply be done by running the CAS statement and (optionally) naming the session. In this case we will call it CASCONN (if we don’t give it an explicit name the session will, by default, be called CASAUTO).

 

/* Connect to the cas server and call the connection casconn*/

cas casconn;

 

Discover which data sets are available

 

SAS Viya for Learners has a large number of sample data sets preloaded and organized into caslibs (libraries on the CAS  server). We can discover what they are by assigning SAS librefs for all the existing caslibs. We can also run Proc Caslib to get details on the data sets held in a specific caslib.

 

/* Assign SAS librefs for all existing caslibs */

caslib _all_ assign;

/* List the tables in one of the sample caslibs */

proc casutil;
	list tables incaslib="busana4m";
run;

ScShot2.png

 

ScShot3.png

 

ScShot4.png

 

Perform our first analysis

 

As previously mentioned, CAS actions operate on the data sets in the CAS server and are grouped into action sets. We will use the COUNTRIES data set in the BUSANA4M caslib and run the CORRELATION action in the SIMPLE action set to see if there is any correlation between years of education, UN id and Gross Domestic Product per head. This will produce a matrix showing the Pearson Coefficient for each combination of the variables and is equivalent to running Proc Corr in a non-CAS session. The code is quite simple, although the action set does have many options which you can add to vary the methods used.

 

/* Perform a simple correlation test */

proc cas;
	simple.correlation / 
		table={name="countries" caslib="busana4m"}
		inputs={"gdp_per_capita" "years_of_education" "un_id"};
run;
quit;

ScShot5.png

We can see from the results (highlighted in green) that, not surprisingly, there is a strong positive correlation between years of education and GDP per head and a negative correlation between UN ID  number and GDP per head.

 

Terminate the session

 

Finally, once you have finished your work it is good practice to explicitly terminate your CAS session to free up resources on the server.

 

/* Terminate the session */

cas casconn terminate;

	

 

In future articles we will be taking a deeper look into CAS programming in SAS Viya for Learners but if there are any areas you would like covered or any other questions or comments, please leave a message below.

Comments

Sounds like a great series, thanks.  Looks like Viya for Learners is only available to people affiliated with a university.  Is there a Viya learning environment accessible to independent learners?

Thanks @Quentin  - unfortunately SAS Viya for Learners access is currently limited to those with an accredited university affiliation. Having said that I know SAS is a company which does listen to its customers so if there is enough interest maybe it will be opened up to independent learners in the future.

Version history
Last update:
‎08-12-2022 09:44 AM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags