BookmarkSubscribeRSS Feed

How to use SAS to create UUID values (and optionally, how to validate them)

Started ‎07-31-2025 by
Modified ‎07-31-2025 by
Views 540

You've probably seen UUIDs (also known as as GUIDs or globally unique IDs), even if you didn't realize what they are. They are unique IDs that systems assign to entities or data records or devices that allow processes to track these individually.

 

A UUID or GUID is a 128-bit value, allowing for 2128 (or 3.4028237e+38) possibilities. Computer processes generate these  behind the scenes all of the time for lots of purposes, and among geeks we joke that they will become a scarce resource, like helium. But rest assured: it will take us a long time to go through all of the possible GUID values.

 

When we see GUID values in the wild, they are usually expressed as a series of 32 hexadecimal characters, often containing hyphens like a crazy sort of phone number (as if you might attempt to commit it to memory), which brings the complete representation to 36 characters:

 

8441876b-8db6-49f4-aaea-da7b48c794a7

 

I created the above GUID fresh for this article (it's never been seen before!) using SAS. In the SAS world, I can use the UUIDGEN function to create these as needed from within my SAS program:

 

data wastedGuids;
  length guid $ 36;
  do x=1 to 10;
    guid = uuidgen();
    output;
  end;
run;

 

Result:

UUIDGEN output from SAS on WindowsUUIDGEN output from SAS on Windows

The UUIDGEN function relies on code libraries that are system-dependent, as there are different algorithms for creating GUID values. When I run the above program on SAS Viya (running on Linux), I get a different pattern of results:

 

UUIDGEN function on SAS ViyaUUIDGEN function on SAS Viya


Validating UUIDs with regex and PRXMATCH

As this discussion illustrates, unique IDs play an important role in data management. But what if someone hands you a collection of values that they claim are UUIDs, but you want to verify they are the real deal? While you cannot verify the uniqueness, you can at least verify that they comply with the format that a true uuidgen tool would have created. UUIDs have a standard specification with several versions, but a general regular expression can provide at least superficial validation.

 

/* a couple of these won't pass */
data fakers;
  infile datalines;
  length uuid $200;
  input uuid;
  retain re;

  if _N_ = 1 then
    re = prxparse('/^([0-9A-Fa-f]{8}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{12})$/');
  is_valid = prxmatch(re, trim(uuid));
  drop re;
  datalines;
c2b155da-c246-5340-z41f-99983cd0e204
328e9f6f-439f-ca4c-a4f5-295bd5ed7249
170621b2-5098-d44e-a44a-07ad2639f847
1d285d54-76e9-8d46-8093-y220b61a9023
effc1f3a-7eaa-e34b-99f3-05742e221fbc
7c0da562-6bbb-554a-a91d-bcbdc19bcb29
3cbfc900-61be-7f44-b0fc-e4c3a7e595c0
ece22c5f-aaeb-ff41-92b2-3a4f67a42dbd
c1ba11bd-4027-0747-bcdb-2a9e8899bca4
0fb9c948-63bd-6346-b5bd-2744d39340b8
;
run;

 

Result:
UUIDs with some impostersUUIDs with some imposters 

 

(Credit: I snagged the working regex expression from a discussion on StackOverflow. You can also workshop regular expressions in regex101.com or similar tools, and adapt them quickly for use in SAS.)

 

Note: I adapted some of this article content from a previous blog post.

Contributors
Version history
Last update:
‎07-31-2025 02:35 PM
Updated by:

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags