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

Hi frnds...
I have a problem that could be a simple one.
In a dataset I have two variables id and spend
like
data a;
input id spend;
cards;
1 100
1 200
1 300
2 150
2 690
;
run;

I want output like

id jan feb mar
1 100 200 300
2 150 690 .

through arrays or sql  share ur simplest code ..thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

How about:

data have;
input id spend;
cards;
1 100
1 200
1 300
2 150
2 690
;
run;
data want;
 set have;
 by id;
 array x{*} jan feb mar;
 retain jan feb mar     ;
 if first.id then do;n=0;call missing(of x{*});end;
 n+1;
 x{n}=spend;
 if last.id;
 drop n spend;
run;


Xia Keshan

View solution in original post

2 REPLIES 2
Ksharp
Super User

How about:

data have;
input id spend;
cards;
1 100
1 200
1 300
2 150
2 690
;
run;
data want;
 set have;
 by id;
 array x{*} jan feb mar;
 retain jan feb mar     ;
 if first.id then do;n=0;call missing(of x{*});end;
 n+1;
 x{n}=spend;
 if last.id;
 drop n spend;
run;


Xia Keshan

naveen_srini
Quartz | Level 8

Same as Xia:

data have;

input id $ spend;

cards;

1 100

1 200

1 300

2 150

2 690

;

data want(drop=count spend);

set have ;

by id;

array mon(3) jan feb march;

retain mon;

if first.id then do;

call missing(of mon{*});

       count=0;

            end;

           count+1;

mon(count)= spend;

if last.id then output;

run;

Thanks,

Naveen Srinivasan

L&T infotech

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 2 replies
  • 1413 views
  • 3 likes
  • 3 in conversation