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

Hi Guys,

 

I have a dataset which looks a bit like this - 

 

AccountNoAccounIDFistNameSecondNameAddress1Address2
11DavidSmith1 Any StAnytown
21Sharon Stevens12 Somewhere St Somewhere
22PeterStevens  
23SamuelStevens  
31Robert Brown44 Elsewhere StElsewhere
32Janice Black  
41SengaLafferty7 Overthere StOverthere
42JohnYoung  

 

So for each AccountNo, when AccountID = 1 all the data is present.

 

How do copy the data for AccountID = 1 into the blank fields for AcountID's > 1?

 

hope that makes sense,

 

best regards,

 

paul

1 ACCEPTED SOLUTION

Accepted Solutions
DanielSantos
Barite | Level 11

Hi.

 

Not tested, but if all data is always at AccountID = 1 and there is no duplicates by AccountNo, AccountID then this may work.

 

 

proc sql noprint;
     create table WANT as
     select a.*, b.Address1, b.Address2
     from HAVE (drop = Address1 Address2) as a left join
          HAVE (keep = AccountNo AccountID Address1 Address2) as b
     on a.AccountNo = b.AccountNo and b.AccountID = 1;
quit;

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post

4 REPLIES 4
ballardw
Super User

RETAIN allows you to keep values from one record to the next. So create temporary variables to hold them when assigned and assign as needed.

 

data want;
   set have;
   length laddress1 laddress2 $ 25; /*<= length should be same as the original address variables*/
   retain laddress1 laddress2;
   if not missing(address1) then laddress1=address1;
   if not missing(address2) then laddress2=address2;
   if  missing(address1) then address1=laddress1;
   if  missing(address2) then address2=laddress2;
   drop laddress1 laddress2;
run;
DanielSantos
Barite | Level 11

Hi.

 

Not tested, but if all data is always at AccountID = 1 and there is no duplicates by AccountNo, AccountID then this may work.

 

 

proc sql noprint;
     create table WANT as
     select a.*, b.Address1, b.Address2
     from HAVE (drop = Address1 Address2) as a left join
          HAVE (keep = AccountNo AccountID Address1 Address2) as b
     on a.AccountNo = b.AccountNo and b.AccountID = 1;
quit;

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

mkeintz
PROC Star
Consider:

Data want;
SET have;
P=1;
If cats(address1,address2)='' then set have (keep=address1 address2) point=p;
Run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
pandhandj
Obsidian | Level 7

thankyou to everyone who took the time to help me with this one.  Your efferts are very much appreciated.

 

thanks,

 

paul

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1760 views
  • 1 like
  • 4 in conversation