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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 464 views
  • 2 likes
  • 6 in conversation