Help using Base SAS procedures

Need help with merging data into an array

Reply
Occasional Contributor
Posts: 19

Need help with merging data into an array

Hello,

I am trying to merge some datasets into one, (5 to 1) to be exact.  Four of the datasets have 3 variables idnum, attemptresult and numbertimes.

The other dataset is the master which has all of these varaibles and several others.  They all have  idnum in common to merge by.  What I would like to do is create

data combine;

merge one two three four five;

by cardid;

run;

Since I have two variables in common attemptresult and numbertimes I wanted to build an array to house those guys

array attresult_array {5} $15. attresult1-attresult5;

array numtime_array {5} 8. numtime1-numtime5;

do i = 1 to 5;

attresult_array{i}=attemptresult;

numtime_array{i}=numtimecalled;

end;

run;

Not sure how to put this all together.  I tried placing the array creation right after the data combine line, and then the do to end after the by idnum line.  It didn't work, the array was empty. Can someone tell me how to accomplish this task?

data combine;

array attresult_array {5} $15. attresult1-attresult5;

array numtime_array {5} 8. numtime1-numtime5;

merge one two three four five;

by cardid;

do i = 1 to 5;

attresult_array{i}=attemptresult;

numtime_array{i}=numtimecalled;

end;

run;

run;

Super User
Posts: 19,770

Re: Need help with merging data into an array

I don't think that will work the way you expect, the merge will overwrite the variable before you place it in the array.

This may be an example of a better place for a sql join. It does seem like no matter what, there's a bit of manual coding unfortunately.

Super User
Posts: 3,250

Re: Need help with merging data into an array

Something like this might do the trick - I'm assuming there is only 1 record per cardid in each input dataset:

data combine;

array attresult_array {5} $15. attresult1-attresult5;

array numtime_array {5} 8. numtime1-numtime5;

retain attresult1-attresult5 numtime1-numtime5;

set one two three four five;

by cardid;

if first.cardid then i = 0;

i + 1;

attresult_array{i}=attemptresult;

numtime_array{i}=numtimecalled;

if last.cardid;

run;

Occasional Contributor
Posts: 19

Re: Need help with merging data into an array

That did the trick.  Thanks!

Super User
Super User
Posts: 7,039

Re: Need help with merging data into an array

Sounds more like a job for the RENAME dataset option.

data want ;

merge

  one (rename=(attemptresult=attresult1 numbertimes=numtime1))

  two (rename=(attemptresult=attresult2 numbertimes=numtime2))

  three (rename=(attemptresult=attresult3 numbertimes=numtime3))

  four (rename=(attemptresult=attresult4 numbertimes=numtime4))

  five (rename=(attemptresult=attresult5 numbertimes=numtime5))

;

by cardid;

run;


Ask a Question
Discussion stats
  • 4 replies
  • 301 views
  • 2 likes
  • 4 in conversation