DATA Step, Macro, Functions and more

Why Variable value retaining???

Accepted Solution Solved
Reply
Super Contributor
Posts: 276
Accepted Solution

Why Variable value retaining???

Hello All..

As i was going through in data step processing and testing some sample SAS programs to check the way of datastep processing.

As i read in the SAS documentation ..

" the variable values in the program data vector are re-set to missing.".


But I am bit confused after looking the olg for bellow program ,the variable count value was retaining ,since i am not using RETAIN statement for that variable.


here i am posting my code and log as well..



CODE:

          Data test;

          Put _all_;

          Input ID fee Name $;

          Count+1;

          Put _all_;

          Cards;

          100 1500 Sanju

          100 2000 kuridi

          100 3000 kumar

          102 4000 sri

          102 5200 nath

          103 9512 sree

          ;

          run;

LOG:

ID=100 fee=1500 Name=Sanju count=1 _ERROR_=0 _N_=1

ID=. fee=. Name=  count=1 _ERROR_=0 _N_=2

ID=100 fee=2000 Name=kuridi count=2 _ERROR_=0 _N_=2

ID=. fee=. Name=  count=2 _ERROR_=0 _N_=3

ID=100 fee=3000 Name=kumar count=3 _ERROR_=0 _N_=3

ID=. fee=. Name=  count=3 _ERROR_=0 _N_=4

ID=102 fee=4000 Name=sri count=4 _ERROR_=0 _N_=4

ID=. fee=. Name=  count=4 _ERROR_=0 _N_=5

ID=102 fee=5200 Name=nath count=5 _ERROR_=0 _N_=5

ID=. fee=. Name=  count=5 _ERROR_=0 _N_=6

ID=103 fee=9512 Name=sree count=6 _ERROR_=0 _N_=6

ID=. fee=. Name=  count=6 _ERROR_=0 _N_=7

Could any one explain me why Count variable still have some value when all other variables having missing values in the log.

Seeking for experts explanation..

Thanks&Regards.

Sanjeev.K






Accepted Solutions
Solution
‎05-02-2013 10:12 AM
Super User
Posts: 10,020

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

Then change:

if id ne lag(id) then count=0;

count+1;

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

Did you happen to look a the documentation for the sum statement?

Super User
Posts: 10,020

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

count + 1 ;

<=>

retain count 0;

count=count+1;

Super Contributor
Posts: 276

Re: Why Variable value retaining???

Thank you,Got it.

In my above code i am trying to generate cumulative count variable per BY group(ID in my above code).it can be possible in the next datastep by using first. and last. automatic variables.

but Is it possible to get this in the same datastep??

Regards.

Sanjeev.K

Respected Advisor
Posts: 3,156

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

change "count+1;" to "Count+1+(-count*(id ne lag(id)));"

Haikuo

Super User
Posts: 10,020

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

and one more way:

if first.id then count=0;

count+1;

Super Contributor
Posts: 276

Re: Why Variable value retaining???

Yes ..you are right..

but it can not be possible in the same datastep,i guess.

Solution
‎05-02-2013 10:12 AM
Super User
Posts: 10,020

Re: Why Variable value retaining???

Posted in reply to kuridisanjeev

Then change:

if id ne lag(id) then count=0;

count+1;

Super Contributor
Posts: 276

Re: Why Variable value retaining???

You make it So simple..

Have to learn a lot from you Mr...

Thank you all ...

Regards.

Sanjeev.K

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 365 views
  • 6 likes
  • 4 in conversation