Hello,
Can we give SAS program line number as sequence by any chance?
Thanks,
nyalamadugu
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 (%)');
@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;
@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.
I would like to use line number as sequence, is it possible?
Thanks,
It is possible. The method to display the line number depends on your SAS platform.
What SAS platform are you using?
@chinna0369 wrote:
I would like to use line number as sequence, is it possible?
Thanks,
No. What are you actually trying to do?
You could create a macro variable and increment it each time before running another step.
@chinna0369 wrote:
I would like to use line number as sequence, is it possible?
Thanks,
Repeat: Use it to do what?
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.
Thanks,
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.
** 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);
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)
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 (%)');
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.