DATA Step, Macro, Functions and more

How do I find the number of observations (not dimension) of an array?

Reply
Contributor
Posts: 34

How do I find the number of observations (not dimension) of an array?

Hello,

 

So I have an array like this:

 

data tempo;
  input employee_id:6.
        last_name:$10.
        birthday:date7.;
  format employee_id:6.
         last_name:$10.
         birthday:date7.;
  datalines;
  1247 Garcia 04APR54
  1078 Gibson 23APR36
  1005 Knapp 06OCT38
  1024 Mueller 17JUN53
;
run;

data tempo2;
	set tempo;
	array temp[*] employee_id;
	d=dim(temp);
	put (temp[*]) (=);
run;

The array's dimension is 1, but it has 4 observations. How do I get the number of observations for use somewhere else? is there a function or another method for obtaining it?

 

Thanks in advance.

Super User
Posts: 23,771

Re: How do I find the number of observations (not dimension) of an array?

In SAS an array is only a shortcut list of variables so I don't think it will give you what you want here. 

 

Dimension will always be 1,  because you only have one variable. 

 

If you explain what you're trying to do, we may be able to suggest alternative approaches.

 


@Datino wrote:

Hello,

 

So I have an array like this:

 

data tempo;
  input employee_id:6.
        last_name:$10.
        birthday:date7.;
  format employee_id:6.
         last_name:$10.
         birthday:date7.;
  datalines;
  1247 Garcia 04APR54
  1078 Gibson 23APR36
  1005 Knapp 06OCT38
  1024 Mueller 17JUN53
;
run;

data tempo2;
	set tempo;
	array temp[*] employee_id;
	d=dim(temp);
	put (temp[*]) (=);
run;

The array's dimension is 1, but it has 4 observations. How do I get the number of observations for use somewhere else? is there a function or another method for obtaining it?

 

Thanks in advance.


 

Contributor
Posts: 34

Re: How do I find the number of observations (not dimension) of an array?

Thank you. I have an ID and a last_modification_dt variables, each loaded in an array. For each ID there are several last_modification_dt values.

 

What I want to do is compare obs1 date versus obs2 date, if the difference between them is less than 7 days, increase a counter and do obs2 date versus obs3 date and so on.

 

It has to be done with arrays.

Super User
Posts: 23,771

Re: How do I find the number of observations (not dimension) of an array?

Why does it have to be done with arrays? Your sample data has no relation to your question below so please provide additional data that reflects your situation and what you're expecting as output. Since this is likely homework, include anything you've tried so we can point you in the right direction. 

 


@Datino wrote:

Thank you. I have an ID and a last_modification_dt variables, each loaded in an array. For each ID there are several last_modification_dt values.

 

What I want to do is compare obs1 date versus obs2 date, if the difference between them is less than 7 days, increase a counter and do obs2 date versus obs3 date and so on.

 

It has to be done with arrays.


 

Contributor
Posts: 34

Re: How do I find the number of observations (not dimension) of an array?

Yeah sorry, it's part of a more complex excercise but I didn't want to ask for the whole solution and figure out some things on my own.

 

The problem is the one I described in my last post.

 

There's one ID variable and one last_modification_dt variable. Some IDs have several last modification dates. For each ID with more than one last_modification_dt, I have to find out if the difference between them is less than 7 days, and how many 'less than 7 days diff' are there for each ID.

 

I thought I would be able to do it with an array but there doesn't seem to be a simple way for me to reference each observation inside the variable in it.

Super User
Posts: 23,771

Re: How do I find the number of observations (not dimension) of an array?


@Datino wrote:

Yeah sorry, it's part of a more complex excercise but I didn't want to ask for the whole solution and figure out some things on my own.

 

The problem is the one I described in my last post.

 

There's one ID variable and one last_modification_dt variable. Some IDs have several last modification dates. For each ID with more than one last_modification_dt, I have to find out if the difference between them is less than 7 days, and how many 'less than 7 days diff' are there for each ID.

 

I thought I would be able to do it with an array but there doesn't seem to be a simple way for me to reference each observation inside the variable in it.


You should be able to use DIF() and or RETAIN, though a SQL join is probably easier. If you provide sample data and expected output you'll likely get a solution.

PROC Star
Posts: 1,831

Re: How do I find the number of observations (not dimension) of an array?

[ Edited ]

May be with nobs= in set statement?

 

data tempo;
  input employee_id:6.
        last_name:$10.
        birthday:date7.;
  format employee_id:6.
         last_name:$10.
         birthday:date7.;
  datalines;
  1247 Garcia 04APR54
  1078 Gibson 23APR36
  1005 Knapp 06OCT38
  1024 Mueller 17JUN53
;
run;

data tempo2;
	set tempo nobs=nobs;
	array temp[*] employee_id;
	d=dim(temp);
	put (temp[*]) (=) nobs=;
run;

 

Either way, nobs= is equal number of obs in dataset at compile time

Super User
Posts: 23,771

Re: How do I find the number of observations (not dimension) of an array?

Posted in reply to novinosrin
nobs will not account for missing values.
Ask a Question
Discussion stats
  • 7 replies
  • 101 views
  • 0 likes
  • 3 in conversation