Help using Base SAS procedures

_N_ not working

Reply
Regular Contributor
Posts: 161

_N_ not working

Hello,

I've got another problem, I am using _N_ to have a count for my observations but for some reason it does not give me the correct count

Thanks
R.A.G
my Code:
data class (rename=(skills1-skills&num_skills=attributes1-attributes&num_skills));
set all_skills1;
array skills{&num_skills} skills1-skills&num_skills;
num=%eval(&num_skills);
if _type_=(2**num)-1 and sum(of skills1-skills&num_skills) ne .;

/*Lambda Intercept*/
num=1;
class=_n_;/****************************HOW???*************/
/*Lambda Main Effect*/
drop num _type_ _freq_ num;

run;
Super Contributor
Super Contributor
Posts: 3,174

Re: _N_ not working

What is the value you are getting? Have you included this statement to verify with accuracy:

PUTLOG _ALL_;

Scott Barry
SBBWorks, Inc.
Regular Contributor
Posts: 161

Re: _N_ not working

the values I am getting starts from 66 and ends with 81, I have 4 variables with 16 observations.
Thanks
Super Contributor
Posts: 394

Re: _N_ not working

_N_ counts the number of times the DATA step has iterated. It is not a count of observations.

See the doc about Automatic Variables.
Frequent Contributor
Posts: 81

Re: _N_ not working

_N_ is the count of observation read from the input dataset and not the observation written to output dataset.

In your case, you are subsetting the data and then assigning the value of _N_ to class variable. How about replacing the statement "class=_n_;" with "class+1;"
Regular Contributor
Posts: 161

Re: _N_ not working

thanks, that worked, but why?
thanks again
R.A.G
Super User
Posts: 9,681

Re: _N_ not working

Because you have "if _type_=(2**num)-1 and sum(of skills1-skills&num_skills) ne .; ".


"if _type_=(2**num)-1 and sum(of skills1-skills&num_skills) ne .; " is actually ""if _type_=(2**num)-1 and sum(of skills1-skills&num_skills) ne . then output;"

That means only obs 66 - 81 suited this condition (i.e. _N_ = 66 - 81),only obs 66 - 81 will output dataset.


Ksharp
Ask a Question
Discussion stats
  • 6 replies
  • 266 views
  • 0 likes
  • 5 in conversation