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

data one;

  length var1 $8. pg 3.;

  input var1 pageNum;

datalines;

subjid   1

usubjid 2

aval      3

subjid  5

;

run;

 

what I am tryin to do is when var1 is the same it will add the two pageNum together separated by a comma:

 

output:

subjid 1,5

usubjid 2

aval  3

 

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

Here's how I would do it (note the correction of variable name pg to pagenum...)

 


data one;
  length var1 $8. pagenum 3.;
  input var1 pageNum;
datalines;
subjid   1
usubjid 2
aval      3
subjid  5
;
run;

proc sort data=one;
by var1 pagenum;
run;

data one_output;
set one;
by var1;
attrib pagenums length=$ 20;
retain pagenums first_pagenum;
if first.var1 then do;
   first_pagenum = pagenum;
   call missing(pagenums);
   end;
pagenums = catx(',', pagenums, strip(putn(pagenum, 'best.')));
if last.var1;
keep var1 pagenums first_pagenum;
run;

proc sort data=one_output out=one_output(keep=var1 pagenums);
by first_pagenum var1;
run;

I retained the lowest pagenum per var1 (as first_pagenum), so I could sort by that to retain the order in your output dataset. 

 

Extend the length of the character variable pagenums if you have many more references.

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

do you mean sort and vertical concatentation with delim ','?

novinosrin
Tourmaline | Level 20
data one;

  input var1 : $8. pg ;
datalines;
subjid   1
usubjid 2
aval      3
subjid  5
;
proc sort data=one;
by var1 pg;
run;

data want;
set one;
by var1;
length want $25;
retain want;
if first.var1 then call missing(want);
want=catx(',',want,pg);
if last.var1;
drop pg;
run;
LaurieF
Barite | Level 11

Here's how I would do it (note the correction of variable name pg to pagenum...)

 


data one;
  length var1 $8. pagenum 3.;
  input var1 pageNum;
datalines;
subjid   1
usubjid 2
aval      3
subjid  5
;
run;

proc sort data=one;
by var1 pagenum;
run;

data one_output;
set one;
by var1;
attrib pagenums length=$ 20;
retain pagenums first_pagenum;
if first.var1 then do;
   first_pagenum = pagenum;
   call missing(pagenums);
   end;
pagenums = catx(',', pagenums, strip(putn(pagenum, 'best.')));
if last.var1;
keep var1 pagenums first_pagenum;
run;

proc sort data=one_output out=one_output(keep=var1 pagenums);
by first_pagenum var1;
run;

I retained the lowest pagenum per var1 (as first_pagenum), so I could sort by that to retain the order in your output dataset. 

 

Extend the length of the character variable pagenums if you have many more references.

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