DATA Step, Macro, Functions and more

Filling blank variables with data from same dataset

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Filling blank variables with data from same dataset

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


Accepted Solutions
Solution
‎02-17-2017 08:58 AM
Super Contributor
Posts: 474

Re: Filling blank variables with data from same dataset

[ Edited ]
Posted in reply to pandhandj

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


All Replies
Super User
Posts: 11,343

Re: Filling blank variables with data from same dataset

Posted in reply to pandhandj

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;
Solution
‎02-17-2017 08:58 AM
Super Contributor
Posts: 474

Re: Filling blank variables with data from same dataset

[ Edited ]
Posted in reply to pandhandj

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

Trusted Advisor
Posts: 1,022

Re: Filling blank variables with data from same dataset

Posted in reply to pandhandj
Consider:

Data want;
SET have;
P=1;
If cats(address1,address2)='' then set have (keep=address1 address2) point=p;
Run;
Contributor
Posts: 28

Re: Filling blank variables with data from same dataset

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

 

thanks,

 

paul

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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