BookmarkSubscribeRSS Feed
TacoLover
Calcite | Level 5

Hi All,

 

I have a simple dataset. Please see below.

How can I load the second observation into an array using a second data step?

 

data have;
infile DATALINES dsd missover;
input varr1 varr2 varr3;
CARDS;
1, 2, 3
2, 3, 4
5, 4
4, 3
9, 4, 1
6,
;run;

 

Thanks!

5 REPLIES 5
Patrick
Opal | Level 21

With SAS9.4 an array is nothing more than a logical pointer to a collection of variables. Only Viya CASL introduces an Array data type for list processing.

With SAS 9.4/Viya Compute here how an array definition looks like.

data have;
  infile DATALINES dsd missover;
  input varr1 varr2 varr3;
  array myarr{*} varr1 - varr3;
  CARDS;
1, 2, 3
2, 3, 4
5, 4
4, 3
9, 4, 1
6,
;

You can also use the array statement to define the variables and then use it in the input statement. The created dataset will just contain variables varr1 to varr2.

data have;
  infile DATALINES dsd missover;
  array myarr{*} varr1 - varr3;
  input myarr[*];
  CARDS;
1, 2, 3
2, 3, 4
5, 4
4, 3
9, 4, 1
6,
;

 

Astounding
PROC Star

Loading an observation into an array is trivial.  So just to make sure we are talking apples to apples, here are some points to consider.

 

Do you expect the array to be saved somehow?  In almost all cases, array definitions disappear as soon as the DATA step ends.

 

Do you really have just 3 variables, or could there be hundreds (or thousands)?  How will you determine the number of variables?

 

Do you need the array values to be available when you process the first observation in the data set?

 

When the program reads the 3rd observation, you may be replacing the contents of the array.  Does that sound OK?

 

The already-asked question of what do you plan to do from here likely becomes relevant.

ballardw
Super User

Why only the second observation? What are you going to do?

Probably just as well to "load" each observation into the array and just use it conditionally.

Amir
PROC Star

Hi,

 

Thanks for using a data step to provide the data, that helps.

 

I would agree with others that some context would help as to why you require a particular observation to be loaded into an array. If you could explain this then you might be presented with another approach that is considered better practice.

 

Having said that, the following code might be one way you could try it (array statement copied from @Patrick).

NB. The use of the stop statement can help prevent a data step from running infinitely when the point= option is being used.

 

data have;
  infile DATALINES dsd missover;
  input varr1 varr2 varr3;
  CARDS;
1, 2, 3
2, 3, 4
5, 4
4, 3
9, 4, 1
6,
;


%let obs_num = 2;

data want(drop = i);
  array myarr{*} varr1 - varr3;
  
  obs_num = &obs_num;
  set have point = obs_num;
  
  do i = 1 to dim(myarr);
    put myarr[i]=;
  end;
  
  output;
  
  /* use stop as point= option is being used */
  stop;
run;

 

The log shows:

 

 85         %let obs_num = 2;
 86         
 87         data want(drop = i);
 88           array myarr{*} varr1 - varr3;
 89           obs_num = &obs_num;
 90           set have point = obs_num;
 91         
 92           do i = 1 to dim(myarr);
 93             put myarr[i]=;
 94           end;
 95         
 96           output;
 97           stop;
 98         run;
 
 varr1=2
 varr2=3
 varr3=4
 NOTE: The data set WORK.WANT has 1 observations and 3 variables.

 

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

So to load just the second observation from a dataset you can use FIRSTOBS= and OBS= dataset options.  To load "into an array" just use the same variable names when listing the members of the array.

 

data want;
  if _n_= 1 then do;
    set have (firstobs=2 obs=2) ;
    array myarray varr1-varr3;
  end;
  /* Now what do you want to do ? */
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1108 views
  • 2 likes
  • 6 in conversation