Tag Archives: SAS Cheat Sheet

SAS Proficiency Test

Here are some common SAS questions:

1- What happens if no VAR statement is used in PROC PRINT?

2- Understand how SAS processes a data step

3- Explain what a (data step) SELECT statement does

4- What is the default length of a numeric variable?

5-What is the difference between DO WHILE and DO UNTIL?

6-What does BY after SET A; do?

7-What does BY after SET A B; do?

8-What is a sub-setting IF statement?

If you pass it doesn’t mean you’re good; but if you’re good, you should pass

9- What’s the difference between INFILE and INPUT?

10-What information is given by a Proc Contents?

11-How do you modify the header?

12-Which elements can you change? (in reference to question 10-11)

13-Which elements can you NOT change without re-creating the dataset?(in reference to question 10-11)

14-What is the purpose of the single trailing @ sign?

15-What statements are used to include or exclude specific variables in a data step?

16-How can you generate a 2-dimensional cross-tabulation?

17-In the macro language, all macro commands begin with?

18-What occurs if multiple datasets are included in a SET statement?

19-What symbol is used to concatenate two character values?

20-Explain the substr function

21-What are the differences among these functions? Round(), Ceil(), Floor()

22-What happens if you use the same in= variable for 2 datasets?

23-How do you begin a data step if you do not want to create a SAS dataset?

24-What are the automatic variables first. and last. ?

25-What does a 2 level input dataset name such as MydataSet.Mydata indicate ?

26-What is the default libname, where one-level dataset names go?

27-What statement allows you to keep track of previous values of variables or keep a running total?

28-What happens if you retain the value of a variable in the incoming dataset?

29-When an invalid data field is encountered when inputting a numeric variable, what happens?

30-What’s the difference between $w. and $CHARw. informats?

31-Explain the _type_ variable generated by proc summary

32-What is the purpose of a %GLOBAL statement?

33-What do CALL SYMPUT and CALL SYMGET do?

34-What date is the reference date for calculating the value of a SAS date variable?

35-What is _n_?

There are 10 kinds of people in the world: those who understand binary, and those who don’t.

Those are SAS most common questions you will be asked during a SAS programming job interview or if you plan in taking the SAS based certification.

In future blogs, I will try to cover each of those questions individually with some demonstrations.

Good luck!

Your comments and questions are valued and encouraged.
Anayansi Gamboa has an extensive background in clinical data management as well as experience with different EDC systems including Oracle InForm, InForm Architect, Central Designer, CIS, Clintrial, Medidata Rave, Central Coding, OpenClinica, Open Source and Oracle Clinical.

 

anayansi gamboa

Advertisements

SAS Cheat Sheet – Part 6

The macro facility is a tool you can use in your programming./*

Macro Language

%DO macro-var=start_value %TO end_value

%DO %WHILE (expression); /*Executes a section of a macro repetitively while a condition is true*/

%DO %UNTIL (expression); /*Executes a section of a macro repetitively until a condition is true*/

Macro variables can be stored in either the global symbol table or in a local symbol table

%GLOBAL macro-variable(s); /*Creates macro variables that are available during the execution of an entire SAS session*/

%IF expression %THEN action; <%ELSE action;> /Conditionally process a portion of a macro*/

%LENGTH (character string | text expression) /*Returns the length of a string*/

The name assigned to a macro variable must be a valid SAS name

%LET macro-variable =<value>; /*Creates a macro variable and assigns it a value*/

%MACRO mname (<pp1><…,ppn><kp1=value<..<kpn=v>);/*Begins a macro definition*/

%MEND <macro-name>; /*Ends a macro definition*/

%SCAN(argument,n<,delimiters>)/*Search for a word that is specified by its position in a string*/

%SUBSTR (argument,position<,length>)/*Produce a substring of a character string*/

%UPCASE (character string | text expression) /*Convert values to uppercase*/

Macro variable values are text values

Macro Quoting

%QUOTE | %NRQUOTE and %BQUOTE | %NRBQUOTE /*Mask special characters and mnemonic operators in a resolved value at macro execution */

%STR | %NRSTR /*Mask special characters and mnemonic operators in constant text at macro compilation */

%SUPERQ /*Masks special characters/mnemonic operators at macro execution but prevents further resolution of the value*/

Example:

*----------------------------------------------------------------*//* This macro will produce summary statistics ----------------   */

%Macro safety1;
%odscmd (start,portnum=11,rptname=AEActivityReport);
Data _null_;
thedate=today();
fstdate = mdy(month(thedate), 1, year(thedate));
lstdate = intnx(‘month’, fstdate, 1)-1;
call symput (‘strtdate’, put(fstdate, date9.));
call symput (‘stpdate’, put(thedate, date9.));
run;

%put strtdate: &strtdate;
%put stpdate: &stpdate;
* more code goes here;

/*----------------------------------------------------------------*/

SAS Cheat Sheet – Part 3

FORMATS

Code Description
<w.d> standard numeric
<COMMAw.d> writes numeric values with commas and decimal points
<Zw.d> print leading zeros
<$w.> writes standard character data
<$CHARw.> writes standard character data
<$VARYINGw.> Writes character data of varying length

Format is used to change how the values of variables are displayed/portrayed in your SAS output. A more formal term for this process is “altering the external representation of the values of variables in a SAS data set.

So how to add leading zeros ? e.g., 999 –> 0999

In order to create variable LEADZEROS_ID, you can use the PUT function and the format Zw. which adds leading zeros to the specified length. Here’s an example:

leadzeros_id=put(id,z4.);

data leadzeros;

input ID PETNAME $ SEX $ AGE WEIGHT;
datalines;
1 Mark M 1 2.5
2 Ace M 3 44
3 Fuzzy M 2 18
4 Champ M 4 55
5 Tom M 6 63.5 62.5
6 Ivon M 7 83
7 Balboa M 12 64.5
;
run;
data leadzeros;
set leadzeros;
leadzeros_num = put(id,z4.); *z4. format is used so 3 leading zeros will be added if the ID length is 1;
run;

Another common format is the COMMAw.d: this format writes numeric values with commas that separate every three digits and a period that separates the decimal fraction.

w = specifies the width of the output field
d = optionally specifies the number of digits to the right of the decimal point in the numeric value

e.g. put @10 profit comma10.2;
so this number 45678.45 will be formatted to 45,678.45