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

Hi all,

 

Am fairly new to SAS and am encountering the following problem. Appreciate if someone could shed some light on it! 🙂

 

I have a dataset which looks like this:

 

SH_NAME refers to the shareholders who own the firm while holding refers to the percentage ownership of the firm. 

REVENUE, PROFIT, ASSETS  & EQUITY figures are that of the firm total.

There are 20 other columns with such firm-level variables. For some variable, there is no info on it (e.g. Equity in the example below).



FIRM_NAMESH_NAMEHOLDINGREVENUEPROFITASSETSEQUITY
AA11001022010
BB117    
BB2171004050 
BB317    
BB417    
BB517    
BB617    

 

Is there a way to fill in the missing blanks such that the final result will look like the following table?

 

FIRM_NAMESH_NAMEHOLDINGREVENUEPROFITASSETSEQUITY
AA11001022010
BB1171004050 
BB2171004050 
BB3171004050 
BB4171004050 
BB5171004050 
BB6171004050 

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way 

 

data have;
infile datalines missover dsd dlm=',';
input FIRM_NAME $ SH_NAME $ HOLDING REVENUE PROFIT ASSETS EQUITY;
datalines;
A,A1,100,10,2,20,10
B,B1,17,
B,B2,17,100,40,50,,
B,B3,17,
B,B4,17,
B,B5,17,
B,B6,17,
;

data temp;
   update have(obs=0) have;
   by FIRM_NAME;
   output;
run;

proc sort data=temp;
   by FIRM_NAME descending SH_NAME; 
run;

data temp2;
   update temp(obs=0) temp;
   by FIRM_NAME;
   output;
run;

proc sort data=temp2 out=want;
   by FIRM_NAME SH_NAME; 
run;

proc datasets lib=work nolist;
   delete temp:;
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Here is one way 

 

data have;
infile datalines missover dsd dlm=',';
input FIRM_NAME $ SH_NAME $ HOLDING REVENUE PROFIT ASSETS EQUITY;
datalines;
A,A1,100,10,2,20,10
B,B1,17,
B,B2,17,100,40,50,,
B,B3,17,
B,B4,17,
B,B5,17,
B,B6,17,
;

data temp;
   update have(obs=0) have;
   by FIRM_NAME;
   output;
run;

proc sort data=temp;
   by FIRM_NAME descending SH_NAME; 
run;

data temp2;
   update temp(obs=0) temp;
   by FIRM_NAME;
   output;
run;

proc sort data=temp2 out=want;
   by FIRM_NAME SH_NAME; 
run;

proc datasets lib=work nolist;
   delete temp:;
run;
tyq1992
Calcite | Level 5

Thanks for the suggestion!

 

However, as i have c.20,000 rows (and around 10,000 blanks that needs to be filled based on the firm ID), writing out each line may take up quite some time, in which case Excel may be a better option to edit the cells?

 

 

 

 

 

PeterClemmensen
Tourmaline | Level 20

I just took your sample data as an example here. My code can handle your 20.000 rows easily 🙂

tyq1992
Calcite | Level 5

Thanks! This worked fine for me! 🙂

Patrick
Opal | Level 21

Based on your sample data below could work.

data have;
infile datalines missover dsd dlm=',';
input FIRM_NAME $ SH_NAME $ HOLDING REVENUE PROFIT ASSETS EQUITY;
datalines;
A,A1,100,10,2,20,10
B,B1,17,
B,B2,17,100,40,50,,
B,B3,17,
B,B4,17,
B,B5,17,
B,B6,17,
;
proc sort data=have;
  by firm_name sh_name;
run;

%let firm_level_vars=REVENUE PROFIT ASSETS;

%let firm_level_vars2=%sysfunc(compbl(&firm_level_vars));
%let firm_level_vars2=%sysfunc(translate(&firm_level_vars2,%str(,),%str( )));
data want;
  merge 
    have(drop=&firm_level_vars) 
    have(keep=firm_name &firm_level_vars 
         where=(n(&firm_level_vars2)>0)
        )
    ;
  by firm_name;
run;

proc print data=want;
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 714 views
  • 0 likes
  • 3 in conversation