BookmarkSubscribeRSS Feed
Sathya3
Obsidian | Level 7

data two;
input name $ qtr amount;
datalines;
adam 1 10
adam 2 20
adam 3 30
adam 4 40
;
run;

 

 

Need a code to produce below output using array and not proc transpose


output must be :

name qtr1 qtr2 qtr3 qtr4
adam 10    20   30    40

3 REPLIES 3
yabwon
Onyx | Level 15
data two;
input name $ qtr amount;
datalines;
adam 1 10
adam 2 20
adam 3 30
adam 4 40
bdam 1 10
bdam 2 20
bdam 4 40
cdam 1 10
cdam 2 20
cdam 3 30
;
run;


data want;
  set two;
  by name;

  array q[4] qtr1-qtr4 (4*.);

  q[qtr] = amount;

  if last.name;
  output;
  call missing(of q[*]);
  
  drop qtr amount;
run;
                         
proc print;
run;

This works, but do consider transpose.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

Why not Proc Transpose? Below one option

data have;
  input name $ qtr amount;
datalines;
adam 1 10
adam 2 20
adam 3 30
adam 4 40
;
run;

data want(drop=_q amount);
  set have(rename=(qtr=_q));;
  by name;
  array qtr{4} 8;
  retain qtr;
  qtr[_q]=amount;
  if last.name then
    do;
      output;
      call missing(of qtr[*]);
    end;
run;
PaigeMiller
Diamond | Level 26

Why not leave the data untransposed?

 

The only reason I can see you might want to have this data arranged with columns qtr1, qtr2, ... is for a report, and if that's what you want, do not transpose, use PROC REPORT which will work on untransposed data.

 

data two;
input name $ qtr amount;
datalines;
adam 1 10
adam 2 20
adam 3 30
adam 4 40
paul 1 23
paul 2 33
paul 3 41
paul 4 67
;

proc report data=two;
    columns ('Name' name) (' ' amount),qtr;
    define name/' ' group;
    define qtr/'Quarter' across;
    define amount/' ' sum;
run;
--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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