Using Python to work with SAS Viya and CAS

One of the big benefits of the SAS Viya platform is how approachable it is for programmers of other languages. You don't have to learn SAS in order to become productive quickly. We've seen a lot of interest from people who code in Python, maybe because that language has become [...]

The post Using Python to work with SAS Viya and CAS appeared first on The SAS Dummy.

Read More

CTRL+Z does not generate EOF in Windows 10

In Windows 10, when I was trying to generate an EOF for a Java program, the CTRL+Z did not work. After doing some research (and help from forum members), it was determined that there is a new option in the cmd prompt in Windows 10:Check that, relaunch ... Read More

The average bootstrap sample omits 36.8% of the data

Suppose you roll six identical six-sided dice. Chance are that you will see at least one repeated number. The probability that you will see six unique numbers is very small: only 6! / 6^6 ≈ 0.015. This example can be generalized. If you draw a random sample with replacement from [...]

The post The average bootstrap sample omits 36.8% of the data appeared first on The DO Loop.

Read More

8 new summer reads for SAS users

Summer is here, which means vacations and time at the pool with a good book. If expanding your knowledge is a goal of yours this summer, SAS has a shelf full of new titles becoming available over the next few months. From new editions of classics – such as SAS® for Forecasting [...]

The post 8 new summer reads for SAS users appeared first on SAS Learning Post.

Read More

How to perform an operation on all numeric or all character variables in a SAS data set

In a previous blog, I demonstrated a program and macro that could identify all numeric variables set to a specific value, such as 999. This blog discusses an immensely useful technique that allows you to perform an operation on all numeric or all chara... Read More

Keeping your data set clean: Integrity constraints

When I teach my Data Cleaning course, the last topic I cover in the two-day course is SAS Integrity Constraints.  I find that most of the students, who are usually quite advanced programmers, have never heard of Integrity Constraints (abbreviated ICs).  I decided a short discussion on this topic would [...]

The post Keeping your data set clean: Integrity constraints appeared first on SAS Learning Post.

Read More

Advanced ODS Graphics: Range Attribute Maps

You can use a range attribute map to control the mapping of values in a continuous variable to colors. This blog shows you how to use PROC SGPLOT to display multiple plots in the same graph and use range attribute maps. The post Advanced ODS Graphics:... Read More

Video: Create and use lists and tables in SAS/IML

My presentation at SAS Global Forum 2017 was "More Than Matrices: SAS/IML Software Supports New Data Structures." The paper was published in the conference proceedings several months ago, but I recently recorded a short video that gives an overview of using the new data structures in SAS/IML 14.2: If your [...]

The post Video: Create and use lists and tables in SAS/IML appeared first on The DO Loop.

Read More

Python Data Structures

This post explains the data structures used in Python. It is essential to understand the data structures in a programming language. In python, there are many data structures available. They are as follows :
  1. strings
  2. lists
  3. tuples
  4. dictionaries
  5. sets

Python Data Structures

1. Strings

Python String is a sequence of characters.

How to create a string in Python

You can create Python string using a single or double quote.
mystring = "Hello Python3.6"
Hello Python3.6

Can I use multiple single or double quotes to define string?

Answer is Yes. See examples below -

Multiple Single Quotes
mystring = '''Hello Python3.6'''
Hello Python3.6
Multiple Double Quotes
mystring = """Hello Python3.6"""
Hello Python3.6

How to include quotes within a string?
mystring = r'Hello"Python"'

How to extract Nth letter or word?

You can use the syntax below to get first letter.
mystring = 'Hi How are you?'
mystring[0] refers to first letter as indexing in python starts from 0. Similarly, mystring[1] refers to second letter.

To pull last letter, you can use -1 as index.

To get first word
mystring.split(' ')[0]
Output : Hi

How it works -

1. mystring.split(' ') tells Python to use space as a delimiter.

Output : ['Hi', 'How', 'are', 'you?']

2. mystring.split(' ')[0] tells Python to pick first word of a string.

2. List

Unlike String, List can contain different types of objects such as integer, float, string etc.
  1. x = [142, 124, 234, 345, 465]
  2. y = [‘A’, ‘C’, ‘E’, ‘M’]
  3. z = [‘AA’, 44, 5.1, ‘KK’]

Get List Item

We can extract list item using Indexes. Index starts from 0 and end with (number of elements-1).
k = [124, 225, 305, 246, 259]



Explanation :
k[0] picks first element from list. Negative sign tells Python to search list item from right to left. k[-1] selects the last element from list.

To select multiple elements from a list, you can use the following method :
k[:3] returns [124, 225, 305]

Add 5 to each element of a list

In the program below, len() function is used to count the number of elements in a list. In this case, it returns 5. With the help of range() function, range(5) returns 0,1,2,3,4.
x = [1, 2, 3, 4, 5]
for i in range(len(x)):
    x[i] = x[i] + 5
[6, 7, 8, 9, 10]

It can also be written like this -
for i in range(len(x)):
   x[i] += 5

Combine / Join two lists

The '+' operator is concatenating two lists.
X = [1, 2, 3]
Y = [4, 5, 6]
Z = X + Y
[1, 2, 3, 4, 5, 6]

Sum of values of two list
X = [1, 2, 3]
Y = [4, 5, 6]
import numpy as np
Z = np.add(X, Y)
[5 7 9]
Similarly, you can use np.multiply(X, Y) to multiply values of two list.

Repeat List N times

The '*' operator is repeating list N times.
X = [1, 2, 3]
Z = X * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Note : The above two methods also work for string list.

Modify / Replace a list item

Suppose you need to replace third value to a different value.
X = [1, 2, 3]
[1, 2, 5]

Add / Remove a list item

We can add a list item by using append method.
X = ['AA', 'BB', 'CC']
Result : ['AA', 'BB', 'CC', 'DD']

Similarly, we can remove a list item by using remove method.
X = ['AA', 'BB', 'CC']
Result : ['AA', 'CC']

Sort list
k = [124, 225, 305, 246, 259]
Output : [124, 225, 246, 259, 305]

3. Tuple

Like list, tuple can also contain mixed data. But tuple cannot be changed or altered once created whereas list can be modified. Another difference is a tuple is created inside parentheses ( ). Whereas, list is created inside square brackets [ ]

mytuple = (123,223,323)
City = ('Delhi','Mumbai','Bangalore')
Perform for loop on Tuple
for i in City:

Tuple cannot be altered

Run the following command and check error
X = (1, 2, 3)
TypeError: 'tuple' object does not support item assignment

4. Dictionary

It works like an address book wherein you can find an address of a person by searching the name. In this example. name of a person is considered as key and address as value. It is important to note that the key must be unique while values may not be. Keys should not be duplicate because if it is a duplicate, you cannot find exact values associated with key. Keys can be of any data type such as strings, numbers, or tuples.

Create a dictionary

It is defined in curly braces {}. Each key is followed by a colon (:) and then values.
teams = {'Dave' : 'team A',
         'Tim' : 'team B',
         'Babita' : 'team C',
         'Sam' : 'team B',
         'Ravi' : 'team C'

Find Values
Output : 'team B'

Delete an item
del teams['Ravi']

Add an item
teams['Deep'] = 'team B'
Output :
{'Babita': 'team C',
 'Dave': 'team A',
 'Deep': 'team B',
 'Sam': 'team B',
 'Tim': 'team B'}

5. Sets

Sets are unordered collections of simple objects.
X = set(['A', 'B', 'C'])

Q. Does 'A' exist in set X?
'A' in X
Result : True

Q. Does 'D' exist in set X?
'D' in X
Result : False

Q. How to add 'D' in set X?
Q. How to remove 'C' from set X?
Q. How to create a copy of set X?
Y = X.copy()
Q. Which items are common in both sets X and Y?
Y & X
Read More

Jedi SAS Tricks - FedSQL Dictionary Tables

Dictionary tables are one of the things I love most about SQL! What a useful thing it is to be able to programmatically determine what your data looks like so you can write self-modifying and data-driven programs. While PROC SQL has a great set of dict... Read More
Back to Top