BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stat34986
Calcite | Level 5

I am trying to create a SAS dataset that looks like the following 

Screenshot 2023-01-09 at 8.42.25 PM.png

I am attempting to use array and a do loop to accomplish this via the following code

 

DATA dat;
    array new(20) X1-X20;
    Do i = 1 to 20;
    	new[i] = new[i] + 1;
    	output;
    end;
RUN;

This code gets me close to the desired dataframe. That is, the position of the missing values are correct however it populates the dataset with all 1s (see below for output). I am not sure what I am doing wrong. I don't understand why new[i[ = new[i] + 1 doesn't increment. 

 

 

Screenshot 2023-01-09 at 8.41.23 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Below should return what you're after.

data dat;
  array new(20) x1-x20;
  do i = 1 to dim(new);
    do k=1 to i;
      new[k]=sum(new[k],1);
    end;
    output;
  end;
run;

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

Below should return what you're after.

data dat;
  array new(20) x1-x20;
  do i = 1 to dim(new);
    do k=1 to i;
      new[k]=sum(new[k],1);
    end;
    output;
  end;
run;
Ksharp
Super User
/*It is IML thing.*/
proc iml;
have=t(1:20);
want=lag(have,0:19);
create want from want[c=('x1':'x20')];
append from want;
close;
quit;

Ksharp_0-1673351004389.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 1245 views
  • 4 likes
  • 3 in conversation