BookmarkSubscribeRSS Feed
data_null__
Jade | Level 19
> Longitudinal Data and SAS: A Programmer’s Guide
>
> Sorry, I wasn't aware that SAS published books by
> "not well informed" authors.
I don't know but it would seem so. I would still like to see program 7-7.

> I gladly bow to your greater knowledge.
I don't claim greater knowledge or experience. I'm just reading the documentation and I see nothing that warrants the cautions that you and SBB are claiming. I have still not seen any examples.

> I guess I have just been hallucinating for the past 25 years of
> SAS programming.
I don't know about that but if you have an example where a RETAIN behaves in a way that is contrary to the documentation I would like to see it.
Flip
Fluorite | Level 6
http://ftp.sas.com/samples/A58176 For 7-7
Sorry, I do not have time to cut out the code I have that was doing this as it is deep in a macro driven system with huge data files.

And I say again, I do not disagree that your technique works for arranging variables. We just need to be aware that there are many beginners on this list who may not be as cognizant of some methods pitfalls. As in several cases I saw in taking the SAS exams, when asked what would happen if you did this, my first responce was, "I would never do that"

So, all I am doing is expressing the need for some caution.
data_null__
Jade | Level 19
> http://ftp.sas.com/samples/A58176 For 7-7

Clearly program 7-7 is too fragile to handle missing times as stated in the document. However it is NOT the "fault" of RETAIN and there a number of simple fixes that would allow it to handle missing TIMES. Seem's like a bad example that could have easily been coded to handle missing times.

> Sorry, I do not have time to cut out the code I have
> that was doing this as it is deep in a macro driven
> system with huge data files.

That's fine I have not interesting in seeing that sort of stuff. I would more interested in an example where RETAIN does not function as documented.

>
> And I say again, I do not disagree that your
> technique works for arranging variables. We just
> need to be aware that there are many beginners on
> this list who may not be as cognizant of some methods
> pitfalls. As in several cases I saw in taking the
> SAS exams, when asked what would happen if you did
> this, my first response was, "I would never do that"
>
> So, all I am doing is expressing the need for some
> caution.

To me the cautions come without proper explanation like that of SBB that I interpreted as sounding like "there is a problem with RETAIN and it must be avoided". Those are the kinds of statements that confuse beginners.
Flip
Fluorite | Level 6
A very simple example of unintended consequenses of inserting a retain statement.

Yes this behaves as documented, but if one is not aware of everything happening in a datastep, stuff happens.

data one;
do x = 1 to 10 ;
output;
end;
run;
data two;
retain y x; * run with and without;
set one(rename = (x = y));
if y = 3 then x = y;
run;
proc print;
run;

I think a middle ground is best, I never say "Never" , just be carefull.
And thanks for the discussion to make us all think.


And now I see why that old macro started acting up: I tried out your technique of putting in a retain to order the variables, there was a rename in the macro. So it retained the value when it got to a missing value. since the datastep has several macro calls and 350 variables and 10**9 records it took a while to find the cause.

So yea, even an old timer like me can make a mistake. Message was edited by: Flip
data_null__
Jade | Level 19
> Hello Data _null_,
>
> Yes I totally agree with you data_null, that Retain
> statement is very easy and effiecient technique to
> rearrange variables. But for starter if he attempts
> to caliculate something in the same dataset then the
> values might retained . For example sake

I see your point. I was actually trying to get the others who fear RETAIN to provide an example.

> Data aa;
> Input Name $3. num1 Num2 ;
> talines;
> Sam 30 32
> Sam . 33
> Sam 33 .
> ;;
> Run;
> /*This Nice and Easy No Problem in Using Retain */;
> Data ss;
> Retain num1 Num2 Name ;
> et aa;
> By Name ;
> n;
> /*A Newbie Like me might do something like this then
> value of Sum would be retained for all the
> Observations right ?? */;
> Data ss1;
> Retain Sum num1 Num2 Name ;
> et Ss; By Name ;
> If First.name then Sum = Num1 +Num2 ;
> un;

This may not be what you wanted but it works as documented.
>
>
> So for this reason I have suggested the Origonal
> Poster proc sql method where the possiblility of
> retain the value would be avoided.

Yes a good alternative but must refer to every variable even if you use *. You can include NOWARN on SQL statement to quiet the noise.

[pre]
35 proc sql nowarn;
36 create table class as
37 select weight,height,* from sashelp.class;
NOTE: Table WORK.CLASS created, with 19 rows and 5 columns.
[/pre]

>
> thanks

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 19 replies
  • 1745 views
  • 0 likes
  • 5 in conversation