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;
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.
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;
That did the trick. Thanks!
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.