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

data have;

input name jan feb mar;

cards;

a   100  200  300

b   100          400

c           100  200

d                   500

;

run;

 

Output Required;

name   jan    feb    mar    concat            Total

a         100    200   300    janfebmar      600

b         100             400    janmar           500

c                   100   200    febmar            300 

d                            500    mar                500      

 

I tried this i'm getting total. Unable to get concat.

Please help me out. Many thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

how about this code.

data have;
input name $ jan feb mar;
cards;
a 100  200  300
b 100    .  400
c   .  100  200
d   .    .  500
;
run;
 
data want;
  set have;
  length concat $30;
  array avars{3} jan feb mar;
  do i=1 to dim(avars);
    if avars{i} ne . then concat=cats(concat,vname(avars{3}));
  end;
  total=sum(of avars{*});
  drop i;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input name $ jan feb mar;
infile datalines dlm = '|' dsd;
datalines;
a|100|200|300 
b|100|   |400 
c|   |100|200 
d|   |   |500 
;

data want;
   set have;
   length concat $ 100;
   array m jan feb mar;
   do over m;
      if m then concat = cats(concat, vname(m));
   end;
   total = sum(of m[*]);
run;

 

Result:

 

name  jan  feb  mar  concat     total 
a     100  200  300  janfebmar  600 
b     100  .    400  janmar     500 
c     .    100  200  febmar     300 
d     .    .    500  mar        500  

 

japelin
Rhodochrosite | Level 12

how about this code.

data have;
input name $ jan feb mar;
cards;
a 100  200  300
b 100    .  400
c   .  100  200
d   .    .  500
;
run;
 
data want;
  set have;
  length concat $30;
  array avars{3} jan feb mar;
  do i=1 to dim(avars);
    if avars{i} ne . then concat=cats(concat,vname(avars{3}));
  end;
  total=sum(of avars{*});
  drop i;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 515 views
  • 2 likes
  • 3 in conversation