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

Hello,

 

Can we give SAS program line number as sequence by any chance?

 

Thanks,

nyalamadugu

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Personally I would add that flexibility to the macro in the same way. 

So update the macro to set a dataset name if one is not provided.

...
%if 0=%length(&out) %then %let out=out&nn ;
...

Make sure to add that line after the code that conditionally sets NN.

 

Then call it without a value for the OUT= parameter.

%getnum(var1=TIDRANDC ne '', var2='TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)');

 

View solution in original post

13 REPLIES 13
Tom
Super User Tom
Super User

@chinna0369 wrote:

Hello,

 

Can we give SAS program line number as sequence by any chance?

 

Thanks,

nyalamadugu


What line number? What sequence?

Are you asking how to generate a variable that has values 1,2,3,... for each observation in a SAS dataset?  That is easy to do with a SUM statement.

data want;
  set have;
  seq + 1;
run;
ballardw
Super User

@chinna0369 wrote:

Hello,

 

Can we give SAS program line number as sequence by any chance?

 

Thanks,

nyalamadugu


Give where?

For what specific use?

Provide some example of what you are attempting.

 

If you intend to write branching code then GO TO and LABEL or Link/ Label/return may be what you want in a data step.

chinna0369
Pyrite | Level 9

I would like to use line number as sequence, is it possible?

 

anyalamadugu_1-1609434053754.png

Thanks,

 

Shmuel
Garnet | Level 18

It is possible. The method to display the line number depends on your SAS platform.

What SAS platform are you using?

Tom
Super User Tom
Super User

@chinna0369 wrote:

I would like to use line number as sequence, is it possible?

 

anyalamadugu_1-1609434053754.png

Thanks,

 


No.  What are you actually trying to do?

You could create a macro variable and increment it each time before running another step.

 

ballardw
Super User

@chinna0369 wrote:

I would like to use line number as sequence, is it possible?

 

anyalamadugu_1-1609434053754.png

Thanks,

 


Repeat: Use it to do what?

chinna0369
Pyrite | Level 9

Okay, 

 

I am creating a macro for row counts, so I am giving order number manually like below. But I would like to give those numbers similar to line number instead of changing every time when I add new rows? I am using SAS9.4.

 

anyalamadugu_0-1609436342496.png

 

Thanks,

Tom
Super User Tom
Super User

If you want help with your code post it as text not photographs.  Make sure to use the Insert Code or Insert SAS Code menu icons to get a pop-up window to preserve the formaing.

chinna0369
Pyrite | Level 9
** Get counts and create macro to repeat for every variable;
%macro getnum(out=, var1=, var2=, nn=);
proc sql;
	create table stp1 as
	select distinct trtpn, count(distinct usubjid) as n,
	case when trtpn=1 then strip(put(calculated n, 3.))||" ("||strip(put((calculated n/&n1.)*100, 5.1))||")"
     	 when trtpn=2 then strip(put(calculated n, 3.))||" ("||strip(put((calculated n/&n2.)*100, 5.1))||")"
    	 when trtpn=9 then strip(put(calculated n, 3.))||" ("||strip(put((calculated n/&n3.)*100, 5.1))||")"
         else ""
	end as numm
	from adsl1
	where &var1.
	group by trtpn;
quit;

proc transpose data=stp1 out=stp2;
	var numm;
	id trtpn;
run;

data &out.;
	length v1 v2 v3 v4 $200.;
	set stp2;

	v1=&var2.;
	v2=_1;
	v3=_2;
	v4=_9;

	ord=&nn.;

	keep ord v1-v4;
run;
%mend;

** Call macro to get final output;
** TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%);
%getnum(out=out2, var1=TIDRANDC ne '', var2=%str("TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)"), nn=2);
%getnum(out=out3, var1=TIDRANDC eq '< 1 years', var2=%str("< 1 YEARS"), nn=3);
%getnum(out=out4, var1=TIDRANDC eq '>= 1 years', var2=%str(">= 1 YEARS"), nn=4);
%getnum(out=out5, var1=TIDRANDC eq '', var2=%str("NOT REPORTED"), nn=5);
Tom
Super User Tom
Super User

You didn't provide any data so let's just make some up.

 

data adsl1 ;
  infile cards dsd truncover;
  input trtpn usubjid $ TIDRANDC :$12. ;
cards;
1,1,
1,2,
1,3,
2,4,< 1 years
2,5,< 1 years
9,6,>= 1 years 
;

%let n1=10;
%let n2=10;
%let n3=10;

Let's modify your process to use the macro variable SEQ if the value of NN is not provided.   Note you could create the ORD variable earlier in the process.

%macro getnum(out=, var1=, var2=, nn=);
%if 0=%length(&nn) %then %do;
  %global seq ;
  %let seq=%eval(&seq+1);
  %let nn=&seq;
%end;
proc sql;
create table stp1 as
  select distinct 
         &nn as ord
       , trtpn
       , count(distinct usubjid) as n
       , case when trtpn=1 then catx(' (',calculated n,put((calculated n/&n1.)*100, 5.1)) || ')'
              when trtpn=2 then catx(' (',calculated n,put((calculated n/&n2.)*100, 5.1)) || ')'
              when trtpn=9 then catx(' (',calculated n,put((calculated n/&n3.)*100, 5.1)) || ')'
         else "" end as numm length=200
  from adsl1
  where &var1.
  group by trtpn
  order by 2
;
quit;

proc transpose data=stp1 out=stp2;
  by ord ;
  var numm;
  id trtpn;
run;

data &out.;
  length ord 8 v1 v2 v3 v4 $200.;
  set stp2;
  v1=&var2.;
  v2=_1;
  v3=_2;
  v4=_9;
  keep ord v1-v4;
run;
%mend getnum;

So now your macro calls can look like:

%let seq=0;
%getnum(out=out2, var1=TIDRANDC ne '', var2='TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)', nn=);
%getnum(out=out3, var1=TIDRANDC eq '< 1 years', var2='< 1 YEARS', nn=);
%getnum(out=out4, var1=TIDRANDC eq '>= 1 years', var2='>= 1 YEARS', nn=);
%getnum(out=out5, var1=TIDRANDC eq '', var2='NOT REPORTED', nn=);

And if we combine the 4 outputs:

data all;
  set out2-out5;
run;

We get:

Obs    ord    v1                                                     v2          v3          v4

 1      1     TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)                2 (20.0)    1 (10.0)
 2      2     < 1 YEARS                                                       2 (20.0)
 3      3     >= 1 YEARS                                                                  1 (10.0)
 4      4     NOT REPORTED                                        3 (30.0)

 

 

 

chinna0369
Pyrite | Level 9
Thank you so much for your reply!

Can we also assign output dataset name as out&nn. while calling macro? Like below?

%let seq=0;
%getnum(out=out&nn., var1=TIDRANDC ne '', var2='TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)', nn=);
%getnum(out=out&nn., var1=TIDRANDC eq '< 1 years', var2='< 1 YEARS', nn=);
%getnum(out=out&nn., var1=TIDRANDC eq '>= 1 years', var2='>= 1 YEARS', nn=);
%getnum(out=out&nn., var1=TIDRANDC eq '', var2='NOT REPORTED', nn=);
Tom
Super User Tom
Super User

Personally I would add that flexibility to the macro in the same way. 

So update the macro to set a dataset name if one is not provided.

...
%if 0=%length(&out) %then %let out=out&nn ;
...

Make sure to add that line after the code that conditionally sets NN.

 

Then call it without a value for the OUT= parameter.

%getnum(var1=TIDRANDC ne '', var2='TIME FROM INITIAL DIAGNOSIS TO RANDOMIZATION (%)');

 

Ksharp
Super User
Tools -> Options -> enhanced editor -> General
and select "Show line numbers "

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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