BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sina
Fluorite | Level 6

Hi, everyone! 

I have a dataset that looks like this: 

data have;
input ID TIME PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 PA8 PA9 PA10 PA11 PA12 PA13 PA14 PA15 PA16 PA17 PA18 PA19 PA20 PA21 PA22 PA23;
cards;
1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2   
2 6 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
3 23 1 0 1 0 1 2 1 1 0 1 1 1 1 2 2 2 0 1 1 0 0 0 0 0
;

Basically, each row represents a patient that has a time of follow-up (in years) and an array of PA0-PA23 which represent levels of physical activity during 24 years. 

What I need is a new array of physical activity that is exactly this long (24 columns) and has the exact same values in each column as the PA array, BUT it generates missing values after reaching the value of time. For instance, for the first patient, the array has the same values from PA0 to PA11 but starts putting missing values from PA12 onwards:

1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 . . . . . . . . . . . .

 

Can you tell me how to create this second array?

Thanks 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
array new_pa(24) new_pa1-new_pa24;
array old_pa(24) pa1-pa24;

do i=1 to time;
     new_pa(i) = old_pa(i);
end;

You can set the loop using a variable, in this case time. So, either set all to the same values and then assign missing from time=13 to 24 or you could initialize all to missing and only copy over up to 12. I show the latter in the code above.

View solution in original post

2 REPLIES 2
Reeza
Super User
array new_pa(24) new_pa1-new_pa24;
array old_pa(24) pa1-pa24;

do i=1 to time;
     new_pa(i) = old_pa(i);
end;

You can set the loop using a variable, in this case time. So, either set all to the same values and then assign missing from time=13 to 24 or you could initialize all to missing and only copy over up to 12. I show the latter in the code above.

art297
Opal | Level 21

Did you show what you are currently getting or what you want to get? If it's the latter, go with @Reeza'a suggested code (other than, of course, load the old array with pa0-pa23).

 

However, if it's the former, and the number of array elements changes, I'd go with something like:

data have;
input ID TIME PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 PA8 PA9 PA10 PA11 PA12 PA13 PA14 PA15 PA16 PA17 PA18 PA19 PA20 PA21 PA22 PA23;
cards;
1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2   
2 6 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
3 23 1 0 1 0 1 2 1 1 0 1 1 1 1 2 2 2 0 1 1 0 0 0 0 0
;
data _null_;
  set have (obs=1);
  length forexec $255;
  array havepa(*) pa:;
  call execute('data want(drop=i);set have;array havepa(*) pa:;');
  forexec=catx(' ','array wantpa(',dim(havepa),');do i=1 to',dim(havepa),';');
  call execute(forexec);
  call execute('wantpa(i)=havepa(i);end;run;');
run;

I wish there was a way to use the dim function, directly, to create new arrays of a given size but, if there is, I'm not aware of it.

 

Art, CEO, AnalystFinder.com

 

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