BookmarkSubscribeRSS Feed
Datino
Obsidian | Level 7

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.

7 REPLIES 7
Reeza
Super User

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.


 

Datino
Obsidian | Level 7

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.

Reeza
Super User

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.


 

Datino
Obsidian | Level 7

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.

Reeza
Super User

@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.

novinosrin
Tourmaline | Level 20

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

Reeza
Super User
nobs will not account for missing values.

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
  • 7 replies
  • 862 views
  • 0 likes
  • 3 in conversation