40 Essential Questions on Base SAS Programming for Aspiring Analysts and Data Scientists
Introduction
For analysts and data scientists, few tools are as essential to master as SAS. With its powerful data manipulation, statistical analysis, and graphical capabilities, SAS remains one of the most widely used software packages in enterprise settings.
Recently, Analytics Vidhya conducted an online assessment that tested aspiring analysts and data scientists on their Base SAS programming knowledge. The test covered both theoretical concepts and practical coding skills on topics like reading in and manipulating datasets, merging tables, statistical procedures, graphing, and more.
In this article, we‘ll walk through some of the most important and commonly tested concepts from this assessment. Whether you‘re preparing for a SAS programming interview or just looking to sharpen your skills, these are the need-to-know topics for anyone who works with SAS.
Breakdown of the Test
A total of 977 people participated in the Base SAS programming skill test. Scores ranged from 0 to 35 out of a total of 40 questions. Here are some summary statistics on the overall results:
- Mean score: 17.73
- Median score: 19
- Mode score: 22
- Highest score: 35
As you can see from the distribution of scores below, performance varied quite significantly, with most test takers scoring between 10 and 30 correct answers:
[insert score distribution image]Helpful Study Resources
If you‘re looking to improve your SAS programming skills, Analytics Vidhya offers a wealth of excellent resources, including:
- SAS learning path for business analysts
- Comprehensive tutorial on merging datasets in SAS
- Guide to data exploration using the DATA step and PROC SQL
I highly recommend bookmarking these and referring to them frequently as you work through practice problems.
Deep Dive into Key Test Topics
Now let‘s look at some of the most important concepts and commonly asked questions from the skill assessment, along with detailed explanations of the solutions.
Data Step Processing
The DATA step is used to read in raw input data, perform transformations and computations, and output a new dataset. It‘s important to understand the program data vector (PDV) and the order in which statements are executed.
Sample question:
Which of the following is the value of the variable c in the dataset created by this code?
data work.one;
a = 2;
b = 3;
c = a ** b;
run;
A. 6
B. 9
C. 8
D. None of the above
Correct answer: C
The ** operator raises the first value to the power of the second value. So in this case, c = 2^3 = 8.
The DATA step processes one row at a time, with the values of the PDV being reset after the OUTPUT statement (explicit or implicit). So while a and b are initially set to 2 and 3, their values don‘t accumulate across iterations.
Variable Attributes
Each variable in a SAS dataset has certain attributes like name, type (numeric vs character), length, format, informat, and label. These attributes determine how the variable‘s data is stored, read in, and displayed.
Sample question:
Which of the following options will convert the character variable "Avg" to numeric?
A. input(Avg, 5.2)
B. put(Avg, 5.2)
C. int(Avg, 5.2)
D. Both A and C
Correct Answer: A
The INPUT function converts a character value to numeric, using an informat to specify how the input value should be read. Here a 5.2 informat is used, meaning read the value as a number with 5 total digits and 2 after the decimal point.
The PUT function does the reverse, converting a numeric to character based on a format. INT returns just the integer portion of a number, ignoring any decimal part.
It‘s important to ensure variables have the appropriate type, as otherwise mathematical operations and comparisons may not work as expected. Use PUT and INPUT liberally to convert between character and numeric.
Merging Datasets
Joining together two or more datasets based on a common key variable is a very frequent task in data manipulation. The MERGE statement allows you to combine observations from multiple datasets into a single output dataset.
Sample question:
Which MERGE statement will result in only observations that have key values common to both input datasets being output?
A. merge employee (in=a) salary;
B. merge employee salary;
C. merge employee (in=a) salary (in=b); by name; if a and b;
D. merge employee (in=a) salary; by name; if a;
Correct Answer: C
To perform an inner join and keep only observations with key values in both datasets, use IN= dataset options to create temporary variables indicating whether the dataset contributed to the current observation.
Then in the subsetting IF statement, require both of those temporary variables to be true. This will cause the DATA step to output a row only when the key value was found in both input datasets.
Using IN= on just one dataset or omitting the subsetting IF will result in a full outer join, keeping all observations from all input tables. To perform a left or right join, use IN= on one table and then subset if that variable is true.
Aggregating Data with PROC MEANS
Summarizing and aggregating data is a key part of any analysis. The MEANS procedure is a powerful tool that can quickly compute descriptive statistics on numeric variables.
Sample question:
What does the MAXDEC option in PROC MEANS control?
A. Maximum number of decimal places to display for the mean
B. Maximum number of decimals to display for all statistics
C. Maximum number of decimals used in calculations
D. Maximum number of digits to display
Correct Answer: B
The MAXDEC option sets the number of decimal places shown for all statistics, not just the mean. It affects display output only, not the internal calculations which are performed at full precision.
By default, means are displayed with 4 decimal places, percentiles with 2, and frequencies with 0. MAXDEC= allows overriding these defaults. Note that statistics with higher precision than the MAXDEC setting will be rounded for display purposes.
Some other useful options in PROC MEANS are:
- VAR – specifies which numeric variables to analyze
- CLASS – performs a separate analysis for each level of the classification variable(s)
- TYPES – controls which interactions of CLASS variables to analyze
- OUTPUT – outputs the summary statistics to a new dataset
Visualizing Data with PROC SGPLOT
Effective data visualization is critical for conveying insights and telling stories with data. The SGPLOT procedure allows creating a variety of common charts and plots with a concise, intuitive syntax.
Sample question:
Which PROC SGPLOT statement is used to create a vertical bar chart?
A. HBAR
B. VBAR
C. VBOX
D. HIGHLOW
Correct Answer: B
The VBAR statement produces a vertical bar chart, while HBAR creates a horizontal bar chart. Other commonly used statements are SCATTER for scatterplots, SERIES for line charts, HISTOGRAM and DENSITY for distribution plots, and BOX for box plots.
In addition to specifying the chart type, you‘ll provide dataset options to map variables to the required aesthetics for that chart type. For example:
proc sgplot data=sales;
vbar product / response=revenue stat=sum;
run;
This creates a vertical bar chart from the "sales" dataset, with different products on the category axis, the sum of the "revenue" variable as the heights of the bars, and sum as the statistic (aggregate function) used.
Tips for Mastering SAS
Here are some of my top recommendations for anyone looking to become a proficient SAS programmer:
-
Adopt a consistent coding style and stick to it. Indent your code blocks, align the RUN statements, use descriptive variable names, and include comments to explain complex logic. This will make your code much more readable and maintainable.
-
Pay attention to log messages. Get in the habit of carefully reviewing the log after submitting code, looking for any errors or warnings. When you encounter an issue, don‘t just guess – take the time to thoroughly read the message and look up what it means.
-
Test incrementally. When writing a long program, don‘t try to do everything at once. Write a little bit of code, check the log and results, then move on to the next step once you‘ve confirmed things are working as expected. Mistakes are much easier to catch and fix this way.
-
Practice, practice, practice. There‘s no substitute for hands-on experience. Work through tutorials and problem sets to get comfortable with the common programming constructs, procedures, and functions. Then start doing your own analyses on datasets you‘re interested in.
-
Don‘t be afraid to experiment. Try passing different options to procedures, or doing things a different way than the examples show. Getting hands-on experience with how the language works is the best way to deepen your understanding and expand your capabilities.
Test Your Knowledge
Now that we‘ve reviewed some key concepts, let‘s practice with a few new questions. Answers are provided at the end of the article – no peeking!
-
Which function returns the position of the first occurrence of a substring within a character value?
A. SUBSTR
B. FIND
C. INDEX
D. FINDC -
The following PROC FREQ code is submitted:
proc freq data=emp;
tables gender * jobtitle / norow nocol;
run;
What does the NOROW option do?
A. Suppresses display of gender percentages
B. Suppresses display of jobtitle percentages
C. Suppresses display of total gender frequencies
D. Suppresses display of total jobtitle frequencies
- Given the following dataset:
Exam Score
1 80
2 .
3 75
4 85
Which PROC UNIVARIATE option is used to specify that missing values should be excluded from the analysis?
A. MISSING
B. NOMISS
C. MISSINGOUT
D. EXCLUDE
- The following PROC FORMAT step is submitted:
proc format;
value agegrp
0-30 = ‘Under 30‘
31-50 = ‘31 to 50‘
50-high = ‘Over 50‘;
run;
Which value would be assigned to an observation with age=50 using this format?
A. Under 30
B. 31 to 50
C. Over 50
D. Missing
- How can you specify a default length of 50 for all new character variables within a DATA step?
A. length char $50;
B. length character $50;
C. length $ 50;
D. length all $ 50;
Conclusion
SAS remains one of the most valuable tools for anyone working with data to master. Its combination of flexible data manipulation, powerful analytic procedures, and robust reporting capabilities make it indispensable for many analysts and data scientists.
Investing the time to thoroughly learn Base SAS programming will pay dividends throughout your career. Start with the basics like DATA step processing, work your way up to combining datasets and running common procedures, and then practice solving progressively more challenging problems.
If you have any other favorite SAS learning resources or practice problems, please share them in the comments below! With dedication and practice, you‘ll be well on your way to becoming a SAS expert.
Practice Question Answer Key
- C
- A
- B
- B
- B
How did you do? If you got them all right, great job! If not, don‘t worry – just review the relevant concepts and try again. Mastering SAS takes time and practice, but it‘s a skill that will serve you well throughout your data career.