BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
help_me_i_dumb
New User | Level 1

Could anyone tell me why this sometimes happens if I do not explicitly include an else statement? I was expecting b321 to only equal 1 for nAtBat=321. Most of the time I have no problems with this, but once in awhile I spend half the day troubleshooting and am usually so fed up that I just add all the else statements and say good riddance. But I would like to understand why this happens.

 

To be clear, nAtBat is the 2nd field. I appreciate any input!

 

data bb;
    retain b321 nAtBat;
	set sashelp.baseball;
	if nAtBat=321 then b321=1;
run;

 

 

Screenshot 2024-09-26 150310.png

1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Lapis Lazuli | Level 10

Hi
You are using RETAIN statement.
When the condition is met (nAtBat=321) b321 is set to 1 and RETAIN statement then retaining 1 as an initial value of b321 at each iteration of data step. Without RETAIN statement, b321 would be missing (.) where the condition is not met.
If you need b321=1 only when nAtBat=321, then just remove RETAIN statement. 

 

data bb;
  set sashelp.baseball;
  if nAtBat=321 then b321=1;
run;

 

View solution in original post

5 REPLIES 5
A_Kh
Lapis Lazuli | Level 10

Hi
You are using RETAIN statement.
When the condition is met (nAtBat=321) b321 is set to 1 and RETAIN statement then retaining 1 as an initial value of b321 at each iteration of data step. Without RETAIN statement, b321 would be missing (.) where the condition is not met.
If you need b321=1 only when nAtBat=321, then just remove RETAIN statement. 

 

data bb;
  set sashelp.baseball;
  if nAtBat=321 then b321=1;
run;

 

help_me_i_dumb
New User | Level 1

Thanks! I used retain to order the variables.  

Tom
Super User Tom
Super User

Why did you use a RETAIN statement for B321 if you did not want the values retained?

Why did you use a RETAIN statement for NATBAT when it already exists in the dataset SASHELP.BASEBALL?  Variables that are sources from existing datasets are always already retained (that is why one to many merges work properly)  In your data step the retained value is never used since you immediately read a new value from the dataset when SET statement executes.

 

If you want those two variables to be the first two then why not just use a LENGTH statement instead?  

data want;
  length b321 natbat 8;
  set sashelp.baseball;
...
help_me_i_dumb
New User | Level 1
"Why did you use a RETAIN statement for B321 if you did not want the values retained? Why did you use a RETAIN statement for NATBAT when it already exists in the dataset SASHELP.BASEBALL?"

Because I'm ignorant. That doesn't entitle you to be condescending.






Tom
Super User Tom
Super User

It was a serious question.  I think you answered it one of your later posts where you mentioned using it to set the order of the variables in the dataset.

 

A number of people will use the RETAIN statement for that purpose, but you need to remember that the actual purpose of the RETAIN statement still applied.  So it is best to do that in a step that does nothing else (or at least does nothing conditionally).

 

The reason the RETAIN statement has been found useful in that way is related to how SAS builds up the dataset (what some places in the documentation call the "program data vector") as it first encounters the variable names.  The advantage it has over using a LENGTH statement (or many others that would also introduce the variable name) is that it just set the order, but does NOT at that point force the data step compiler to decide the type and storage length of the variables.  So you can use with out knowing if the variable is numeric or character (and for character how many bytes of storage it needs).  Then when later the data step compiler sees the variable, for example when examining a dataset being read in , it will fix the variable type and length.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 190 views
  • 5 likes
  • 3 in conversation