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
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.
do you mean sort and vertical concatentation with delim ','?
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.