BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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





1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Then change:

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

count+1;

View solution in original post

8 REPLIES 8
data_null__
Jade | Level 19

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

Ksharp
Super User

count + 1 ;

<=>

retain count 0;

count=count+1;

kuridisanjeev
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

Haikuo

Ksharp
Super User

and one more way:

if first.id then count=0;

count+1;

kuridisanjeev
Quartz | Level 8

Yes ..you are right..

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

Ksharp
Super User

Then change:

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

count+1;

kuridisanjeev
Quartz | Level 8

You make it So simple..

Have to learn a lot from you Mr...

Thank you all ...

Regards.

Sanjeev.K

sas-innovate-2024.png

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.

 

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
  • 8 replies
  • 1217 views
  • 6 likes
  • 4 in conversation