BookmarkSubscribeRSS Feed
nickbuon01
Calcite | Level 5

need some help. in the process of writing a short macro. I have a dataset of about 3,000 records. I need to separate each ID, then do some analysis then create a table. I need to go back and get the next id records and do the same and so on. I have the beginning but i do not know how to count the each record set. I have created a rownum for each set

 

of records. any ideas would be useful. Below is an example. and some Do-loop code. I know i will need to figure how to go down the record listof rownum's. Thank you in adavnce. 

 

Dx_Level,Dx_Condition,Dx_level,Count,Countpermonth,allwd pay,allwd expected,Rownum

904,Ischemic heart diseases,51,1715,2366,159617812.6,67463.15,1

904,Ischemic heart diseases,31,18873,41249,177850913.8,4311.64,1

904,Ischemic heart diseases,33,3088,3597,76769181.5,21342.56,1

904,Ischemic heart diseases,52,672,737,63219340.64,85779.3,1

904,Ischemic heart diseases,32,9185,12286,113050036.7,9201.53,1

904,Ischemic heart diseases,42,4264,4949,305570930.4,61743.97,1

904,Ischemic heart diseases,53,1124,1147,301700925,263034.81,1

904,Ischemic heart diseases,43,3366,3629,459381125.8,126586.15,1

904,Ischemic heart diseases,41,8770,14542,421131055.4,28959.64,1

904,Ischemic heart diseases,12,247,260,533137.16,2050.53,1

904,Ischemic heart diseases,13,81,84,164342.09,1956.45,1

904,Ischemic heart diseases,23,247,265,2178525.94,8220.85,1

904,Ischemic heart diseases,21,1819,3739,4042923.97,1081.28,1

904,Ischemic heart diseases,22,333,371,1864851.64,5026.55,1

904,Ischemic heart diseases,0,18859,67472,0,0,1

904,Ischemic heart diseases,11,1396,2434,1367565.15,561.86,1

201,Malignant neoplasm of lip,13,1,1,81202.74,81202.74,2

201,Malignant neoplasm of lip,11,38,54,564851.15,10460.21,2

201,Malignant neoplasm of lip,12,16,18,611165.65,33953.65,2

201,Malignant neoplasm of lip,0,11,38,0,0,2

303,Malignant neoplasm of base of tongue,13,44,49,5139455.73,104886.85,3

303,Malignant neoplasm of base of tongue,0,164,414,0,0,3

303,Malignant neoplasm of base of tongue,12,232,338,8651302.77,25595.57,3

303,Malignant neoplasm of base of tongue,11,498,1059,25156595.02,23755.05,3

 

data want;

  set work.HJICLter;

  count = 1;

  do while (rownum=count);

    if rownum = rownum then count + 1;

    output;

  end;

run;

 

I have more to do. but the above with a counter to go thru separately each rownum and just create one sas table and understand how to move the rownum to the next record would be helpful. 

Thank you. 

 

13 REPLIES 13
PaigeMiller
Diamond | Level 26

So, I don't see how a macro would help here. First, the looping in SAS to create rownum can be made much simpler than your code.

 

data want;
    set have;
    by dx_level notsorted;
    if first.dx_level then rownum+1;
run;

 

As far as performing some "analysis", it would really help if you told us what analysis you want to do. Many analyses can be done without a macro in the appropriate PROC using a BY statement. And so no looping is needed, and no macro is needed. In fact, in that case, you don't even need to compute ROWNUM. Example using PROC MEANS

 

proc means data=have;
    by dx_level notsorted;
    var count countpermonth allwd;
run;

 

By the way, you say "I need to separate each ID" but there is no variable named ID, so its not clear what you mean here.

--
Paige Miller
nickbuon01
Calcite | Level 5
Paige,
Here is the code example:
data want;
set work.HJICLter;
count = 1;
do while (rownum=count);
if rownum = rownum then count + 1;
output;
end;
run;

proc fastclus data=work.test maxclusters=7 out=test_904 noprint;
var Dx_lvl;
id allwd_pay;
run;

proc append;
base=work.final;
data=work.test_904;
run;
Patrick
Opal | Level 21

Just as a side note because the code you shared makes me think you haven't fully grasped yet how a data step works in SAS9.

data want;
  set work.HJICLter;
  count = 1;
  do while (rownum=count);
    if rownum = rownum then
      count + 1;
    output;
  end;
run;

The SET statement will already cause the data step to iterate over the source table (sequential single threaded read, one data step iteration/loop per row in the source table). 

There is an automatic variable _n_ available during data step processing (not saved to the output table) that gets automatically incremented with +1 for each row read from source. 

If you want an output table with a row number variable then simply assign _n_ to this variable. Here an example:

data want;
  set work.HJICLter;
  rownum=_n_;
run;



nickbuon01
Calcite | Level 5
ID is Dx_Level. Also, the rownum separates all Dx_Level as one rownum
PaigeMiller
Diamond | Level 26
proc fastclus data=work.test maxclusters=7 out=test_out noprint;
    by dx_level notsorted;
    id allwd_pay;
run;

 

No looping needed, no macros needed, no PROC APPEND needed. All the output will go into data set TEST_OUT, identified by dx_level number.

--
Paige Miller
PaigeMiller
Diamond | Level 26

PS: @nickbuon01 please don't submit the same question in two different threads.

--
Paige Miller
nickbuon01
Calcite | Level 5
Dx_LevelDx_ConditionDx_levelCountCountpermonthallwd payallwd expectedRownum
904Ischemic heart diseases5117152366159617812.667463.151
904Ischemic heart diseases311887341249177850913.84311.641
904Ischemic heart diseases333088359776769181.521342.561
904Ischemic heart diseases5267273763219340.6485779.31
904Ischemic heart diseases32918512286113050036.79201.531
904Ischemic heart diseases4242644949305570930.461743.971
904Ischemic heart diseases5311241147301700925263034.811
904Ischemic heart diseases4333663629459381125.8126586.151
904Ischemic heart diseases41877014542421131055.428959.641
904Ischemic heart diseases12247260533137.162050.531
904Ischemic heart diseases138184164342.091956.451
904Ischemic heart diseases232472652178525.948220.851
904Ischemic heart diseases21181937394042923.971081.281
904Ischemic heart diseases223333711864851.645026.551
904Ischemic heart diseases01885967472001
904Ischemic heart diseases11139624341367565.15561.861
201Malignant neoplasm of lip131181202.7481202.742
201Malignant neoplasm of lip113854564851.1510460.212
201Malignant neoplasm of lip121618611165.6533953.652
201Malignant neoplasm of lip01138002
303Malignant neoplasm of base of tongue1344495139455.73104886.853
303Malignant neoplasm of base of tongue0164414003
303Malignant neoplasm of base of tongue122323388651302.7725595.573
303Malignant neoplasm of base of tongue11498105925156595.0223755.053

 

Above is an example. I have so far been able to create a do-loop :

but it only separates one rownum example. I have a count for the rownum to be added next. But i am missing how to keep the added count to get to the next rownum .

data want;
set work.HJICLter;
count = 1;
do while (rownum=count);
if rownum = rownum then count + 1;
output;
end;
run;

 

I just need to go back thru and separate each rownum and do separate analysis and then have a final dataset of each rownum appended to a final dataset. 

 

Any help would be appreciated. 

 

HB
Barite | Level 11 HB
Barite | Level 11

I have no idea what you want and because you have no other responses my guess is no one else does either.

 

If you want a count of records by rownum, that is a simple:

 

proc sql;
select
rownum, 
count(rownum)

from 
have

group by
rownum
;
quit; 

But like I say, I have no idea what you want based on what you wrote.  

You have two dx_level fields, allwd pay with a space isn't a valid filed name, etc.

 

Edit:  Plus I see you have two threads with the same question(s)?

nickbuon01
Calcite | Level 5
This is what i want to do. each dx_level has a unique record, i created a rownum at the end. So i have 1,2,3
Below is the code i ma trying to get to for esch rownum to be used in the proc cluster data. example, rownum1, create table, use that table in the cluster take the output table from the cluster and start to create a final data using the proc append.
data wanta;
set work.want;
count = 1;
do while (rownum=count);
if rownum = rownum then count + 1;
output;
end;
run;

proc fastclus data=work.wanta maxclusters=7 out=test_904 noprint;
var Dx_lvl;
id allwd_pay;
run;

proc append;
base=work.final;
data=work.test_904;
run;
PaigeMiller
Diamond | Level 26

Please see my answer in your other thread

https://communities.sas.com/t5/SAS-Programming/Continuous-do-loop-with-counter/m-p/908371

 

Please do not submit the same question in multiple threads. One thread is sufficient.

--
Paige Miller
Tom
Super User Tom
Super User

You don't have an ID number variable.

You appear to have DX_LEVEL and ROWNUM that both identify the same groups of observations.

 

If you want to do the same analysis for all groups then just add a BY statement to you procedure.

 

Since ROWNUM is sorted in ascending order you could use it:

proc means data=have;
  by rownum;
run;

But I suspect that DX_LEVEL is going to be more meaningful in the output.  So you could perhaps just include both:

by rownum dx_level;

Or you could use just dx_level, but add the NOTSORTED keyword to let SAS know they are not in ascending order.

by dx_level notsorted;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2595 views
  • 0 likes
  • 6 in conversation