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.

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