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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.