BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I am trying to figure something out, and the solution is probably very easy. I have a listing of numbers that I would like to see printed as ranges.

Before:
200
201
202*
203
204*
205
206

After:
200 - 201, 203, 205-206

The numbers with asterisk will be printed separately. I have no clue where to start.
Thanks!
6 REPLIES 6
Cynthia_sas
SAS Super FREQ
Hi:
You say you want to see these numbers "printed" -- what do you mean by that? Are you running PROC PRINT, PROC REPORT, PROC TABULATE??? Are you running PROC SQL?? Are you using ODS and if so, what is your destination of interest??

You show a "before" and "after" but do not indicate whether there are any counts or other values underneath your variable values. What is the name of the variable that contains the numbers 200, 201, etc??? What you describe seems to be that you want the variable values (from BEFORE) to become column headers???? If that is the case, do keep in mind that SAS variable names cannot start with a number -- they can only start with a character or an underscore.

You could create a custom report, written to FILE PRINT, using PUT statements, but without more information about your data, what the data represents and where the final "printed" report is going (destination) and what else should be on the final report, it's hard to give a definitive answer.

Can you explain a bit more about this task and the intended usage for the report with the newly printed/rearranged rows????

cynthia
deleted_user
Not applicable
Sorry - the numbers are participant id numbers with the variable name IDNUM. I would like to print just the values of the variable IDNUM to represent a specific event: 200-202, 204 all passed a test. I need to create a report that shows these ranges, instead of each number individually.

Currently I'm using Base SAS and printing them using PROC PRINT. However, I am totally open to trying PROC REPORT or PROC TABULATE. I'm also open to using ODS.
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello LisaB,

This is my solution but I am not sure that this is what you need:.

data a;
length id $4;
input id;
datalines;
200
201
202*
203
204*
205
206
;
run;
/* Creating group variable i */;
data a1;
set a;
if SUBSTR(id,4,1) = "*" then i+1;
else output;
run;
/* Creating variable r for id ranges */;
data a2;
length r1 $3 r $10;
retain r1;
set a1;
if first.i then r1=SUBSTR(id,1,3);
if first.i and last.i then do; r=r1; output; end;
if not first.i and last.i then do; r=r1||"-"||id; end;
by i;
run;
proc print data=a2;
var r;
run;

Sincerely,
SPR
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Sorry, I forgot OUTPUT in the following line of the last data step:

if not first.i and last.i then do; r=r1||"-"||id; OUTPUT; end;

SPR
Ksharp
Super User
Hi.
Suppose you are doing survival statistical analysis.and asterisk denoted the censored data, right?
And also i do not think it is easy to do.Hope somebody gives a brief and directly resolution.
It my rude code.
[pre]
data a;
input id : $;
datalines;
200
201
202*
203
204*
205
206
207
208
209*
210
211
212
213
214
215*
216
;
run;
/* Creating group variable i */
data asterisk data;
set a;
retain i 1;
if findc(id,'*') then do;
i+1;
output asterisk;
end;
else output data;
run;
data result;
set data;
by i;
length result $ 10 firstid $ 4;
retain result firstid;
if first.i and not last.i then firstid=id;
else if not first.i and last.i then do;
result=catx('-',firstid,id);
output;
end;
else if first.i and last.i then do;
result=id;
output;
end;
run;
proc sql noprint;
select result
into :result separated by ','
from result;
quit;
%put the result is : &result;
[/pre]


Ksharp

Message was edited by: Ksharp
chang_y_chung_hotmail_com
Obsidian | Level 7
Here is another way, writing to lst.
[pre]
/* test data */
data one;
input id : $ @@;
datalines;
200 201 202* 203 204* 205 206 207 208
209* 210 211 212 213 214 215* 216
;
run;

/* this assumes that id^s are conjecutive */
data _null_;
length start finish range $20 comma $1;
retain start finish comma "";

set one end=end;

star = index(id,"*") > 0;
if star then do;
if missing(start) then return;
link outRange;
call missing(start, finish);
end; else do;
if missing(start) then start = id;
finish = id;
end;
if end and not missing(start) then link outRange;
return;

outRange:
file print;

len = lengthn(comma);
put comma $varying. len @;

range = ifc(start=finish, start, catx("-", start, finish));
len = lengthn(range);
put range $varying. len @;

comma = ",";
return;
run;
/* on lst
200-201,203,205-208,210-214,216
*/
[/pre]

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1888 views
  • 0 likes
  • 5 in conversation