BookmarkSubscribeRSS Feed
PhilG
Fluorite | Level 6

Hello!

I'm trying to carry non-missing values down a by group for multiple numeric and character columns at the same time, but I'm having trouble. It is carrying forward, but it is applying the same value from the first column to all of the columns. I have a feeling that I'm miss-using the retain command on the temp variable. So I guess the question is: sIs there a way I can update the temp in these do loops so it fills values only from the appropriate column? Here's the syntax I've used:

 

 

 

data have;

input id group rate rate2 name $ number location $;

datalines;

1 1 5 6 Rick . USA

1 1 . . . 2 .

1 1 2 3 Stan . UK

1 2 . . . . . 

1 2 . . . 4 .

1 2 . . Sarah .  Spain

1 2 . . . . .

1 2 . . . . .

1 3 . . . Bf . Poland

1 3 3 . . . .

1 3 . . . . .

 

 

data get;

set have;

by group;

array Nums[*] _numeric_;

array Chars[*] _character_;

do i= 1 to dim(nums);

retain temp;

if first.group then temp=.;

if nums[i] ne . then temp=nums[i];

else if nums[i]=. then nums[i]=temp;

end;

do i= 1 to dim(Chars);

retain temp;

if first.group then temp=.;

if Chars[i] ne . then temp=NUMs[i];

else if Chars[i]=. then Chars[i]=temp;

end; run;

 

 

 

 

 

What I get with the syntax:

 

1 1 5 6 Rick . USA

1 1 5 5 5 2 5

1 1 2 3 Stan 5 UK

1 2 5 5 5 5 5

1 2 5 5 5 4 5

1 2 5 5 Sarah 5 Spain

1 2  5 5 5 5 5

1 2 5 5 5 5 5

1 3 5 5 5 Bf 5 Poland

1 3 3 5 5 5 5

1 3 5 5 5 5 5

 

 

Want:

 

1 1 5 6 Rick . USA

1 1 5 6 Rick 2 USA

1 1 2 3 Stan 2 UK

1 2 . . . . UK 

1 2 . . . 4 UK

1 2 . . Sarah 4  Spain

1 2 . . Sarah 4  Spain

1 2 . . Sarah 4  Spain

1 3 . . Bf . Poland

1 3 3 . Bf . Poland

1 3 3 . Bf . Poland

 

 

As always, I appreciate any assistance!

 

Cheers!

 

P

2 REPLIES 2
HB
Barite | Level 11 HB
Barite | Level 11

Is it the case the the only records that really matter in WANT are 

 

1 1 2 3 Stan 2 UK
1 2 . . Sarah 4  Spain
1 3 3 . Bf . Poland

??

 

PhilG
Fluorite | Level 6
Unfortunately, no. I setup this little test data for demonstration purposes, to show the process I'm attempting. The actual dataset has about 1300 columns and millions of rows.


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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1027 views
  • 0 likes
  • 2 in conversation