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
Did you happen to look a the documentation for the sum statement?
count + 1 ;
<=>
retain count 0;
count=count+1;
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
change "count+1;" to "Count+1+(-count*(id ne lag(id)));"
Haikuo
and one more way:
if first.id then count=0;
count+1;
Then change:
if id ne lag(id) then count=0;
count+1;
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.
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.