DATA Step, Macro, Functions and more

Keep vs Retain Statement

Occasional Contributor
Posts: 17

Keep vs Retain Statement

Could someone tell me the difference between Keep and Retain statements for SAS?



Posts: 42

Re: Keep vs Retain Statement

keeps refers to the variable beeing read or written in the dataset


retain stores the value of a variable between observations, it surpresses reseting of the variable

Super User
Posts: 11,336

Re: Keep vs Retain Statement

In addition ot @chrej5am, Keep (and Drop) can be a data set option and used anywhere a dataset is used to restrict the variables used by a procedure. Retain is only valid in a data step.

Super User
Posts: 7,757

Re: Keep vs Retain Statement

Adding to what @chrej5am said, retain works reliably only with newly created variables. Variables that are already present in the input dataset(s) will be overwritten anytime a new observation is read.

Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation