BookmarkSubscribeRSS Feed
pangea17
Quartz | Level 8

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;

4 REPLIES 4
Reeza
Super User

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.

SASKiwi
PROC Star

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;

pangea17
Quartz | Level 8

That did the trick.  Thanks!

Tom
Super User Tom
Super User

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;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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