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

I have a two part question.

How would I create the table below using proc report? I can't figure out how to split the cells underneath the targets!  

lu_king_1-1718156496567.png

Second question: In my dataset I have one variable 'Target' containing both the target and the strain (Pox S1, Pox S2, etc.) would I still be able to create this table keeping it like that, or should I reshape my data set? 

 

Thanks for the help!

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Have a look at the sample code below. The original target variable contents is split into two variables so that we can use the nesting of the across usage.

 

/*
 * create dummy data
 */
data have;
  infile cards dlm=",";
  input
    week : date9.
    target : $32.
    value : 8.
  ;
  /*
   * split target into two variables
   */
  target2 = scan(target, 1, " ");
  strain = scan(target, 2, " ");
  format week date9.;
cards;
27May2024,Pox S1,19
27May2024,Pox S2,25
27May2024,Ebola A,4
27May2024,Ebola B,.
27May2024,Prion B,.
27May2024,Prion H,.
28May2024,Pox S1,10
28May2024,Pox S2,30
28May2024,Ebola A,4
28May2024,Ebola B,1
28May2024,Prion B,2
28May2024,Prion H,.
;

/*
 * create a format to print missing values
 * as requested
 */
proc format;
  value mynumber 
    . = "not detected"
    other = [8.]
  ;
run;

/*
 * proc report code
 * nocompletecols only print the actual values
 * group usage for week so that multiple rows are reduced to one
 * use of " " as label to suppress printing
 * target2 and strain used as across, the comma in the column
 * statement indicates the nesting
 */
proc report data=have nocompletecols;
  column ("week" week) target2, strain, value;
  define week / " " group;
  define target2 / " " across ;
  define strain / " " across;
  define value / " " analysis format=mynumber. center;
run;

/*
 * alternate way using proc tabulate
 */
proc tabulate data=have format=mynumber.;
class week target2 strain;
var value;

table week = " ", target2 = " " * strain = " " * value = " " * sum = " ";
run;

View solution in original post

1 REPLY 1
BrunoMueller
SAS Super FREQ

Have a look at the sample code below. The original target variable contents is split into two variables so that we can use the nesting of the across usage.

 

/*
 * create dummy data
 */
data have;
  infile cards dlm=",";
  input
    week : date9.
    target : $32.
    value : 8.
  ;
  /*
   * split target into two variables
   */
  target2 = scan(target, 1, " ");
  strain = scan(target, 2, " ");
  format week date9.;
cards;
27May2024,Pox S1,19
27May2024,Pox S2,25
27May2024,Ebola A,4
27May2024,Ebola B,.
27May2024,Prion B,.
27May2024,Prion H,.
28May2024,Pox S1,10
28May2024,Pox S2,30
28May2024,Ebola A,4
28May2024,Ebola B,1
28May2024,Prion B,2
28May2024,Prion H,.
;

/*
 * create a format to print missing values
 * as requested
 */
proc format;
  value mynumber 
    . = "not detected"
    other = [8.]
  ;
run;

/*
 * proc report code
 * nocompletecols only print the actual values
 * group usage for week so that multiple rows are reduced to one
 * use of " " as label to suppress printing
 * target2 and strain used as across, the comma in the column
 * statement indicates the nesting
 */
proc report data=have nocompletecols;
  column ("week" week) target2, strain, value;
  define week / " " group;
  define target2 / " " across ;
  define strain / " " across;
  define value / " " analysis format=mynumber. center;
run;

/*
 * alternate way using proc tabulate
 */
proc tabulate data=have format=mynumber.;
class week target2 strain;
var value;

table week = " ", target2 = " " * strain = " " * value = " " * sum = " ";
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 1 reply
  • 534 views
  • 0 likes
  • 2 in conversation