Exeperts,
I have huge dataset and I want to reorder my row number 1-6.how do I order when I don't have other variable to group.
here is the sample SAS program:
data want;
set sashelp.class;
rownum=_n_;
newvalue=mod(rownum,2);
run;
Here is the expected output:
Name | Sex | Age | Height | Weight | rownum | newvalue |
Alfred | M | 14 | 69.0 | 112.5 | 1 | 1 |
Alice | F | 13 | 56.5 | 84.0 | 2 | 2 |
Barbara | F | 13 | 65.3 | 98.0 | 3 | 3 |
Carol | F | 14 | 62.8 | 102.5 | 4 | 4 |
Henry | M | 14 | 63.5 | 102.5 | 5 | 5 |
James | M | 12 | 57.3 | 83.0 | 6 | 6 |
Jane | F | 12 | 59.8 | 84.5 | 7 | 1 |
Janet | F | 15 | 62.5 | 112.5 | 8 | 2 |
Jeffrey | M | 13 | 62.5 | 84.0 | 9 | 3 |
John | M | 12 | 59.0 | 99.5 | 10 | 4 |
Joyce | F | 11 | 51.3 | 50.5 | 11 | 5 |
Judy | F | 14 | 64.3 | 90.0 | 12 | 6 |
Louise | F | 12 | 56.3 | 77.0 | 13 | 1 |
Mary | F | 15 | 66.5 | 112.0 | 14 | 2 |
Philip | M | 16 | 72.0 | 150.0 | 15 | 3 |
Robert | M | 12 | 64.8 | 128.0 | 16 | 4 |
Ronald | M | 15 | 67.0 | 133.0 | 17 | 5 |
Thomas | M | 11 | 57.5 | 85.0 | 18 | 6 |
William | M | 15 | 66.5 | 112.0 | 19 | 1 |
Appreciate your help.
thanks,
Use the MOD() function.
Reeza,
I am using MOD() function but I am getting 1 and 0.
if you don't mind could you please add your code to example above.
thanks,
Post what you've tried and I'm happy to help.
When I this:
data want;
set sashelp.class;
rownum=_n_;
newvalue=mod(rownum,2);
run;
I got this one.
Name | Sex | Age | Height | Weight | rownum | newvalue |
Alfred | M | 14 | 69.0 | 112.5 | 1 | 1 |
Alice | F | 13 | 56.5 | 84.0 | 2 | 0 |
Barbara | F | 13 | 65.3 | 98.0 | 3 | 1 |
Carol | F | 14 | 62.8 | 102.5 | 4 | 0 |
Henry | M | 14 | 63.5 | 102.5 | 5 | 1 |
James | M | 12 | 57.3 | 83.0 | 6 | 0 |
Jane | F | 12 | 59.8 | 84.5 | 7 | 1 |
Janet | F | 15 | 62.5 | 112.5 | 8 | 0 |
Jeffrey | M | 13 | 62.5 | 84.0 | 9 | 1 |
John | M | 12 | 59.0 | 99.5 | 10 | 0 |
Joyce | F | 11 | 51.3 | 50.5 | 11 | 1 |
Judy | F | 14 | 64.3 | 90.0 | 12 | 0 |
Louise | F | 12 | 56.3 | 77.0 | 13 | 1 |
Mary | F | 15 | 66.5 | 112.0 | 14 | 0 |
Philip | M | 16 | 72.0 | 150.0 | 15 | 1 |
Robert | M | 12 | 64.8 | 128.0 | 16 | 0 |
Ronald | M | 15 | 67.0 | 133.0 | 17 | 1 |
Thomas | M | 11 | 57.5 | 85.0 | 18 | 0 |
William | M | 15 | 66.5 | 112.0 | 19 | 1 |
Why are you using 2 in the MOD() function?
I am trying to divide by 2.
Reeza,
data want;
set sashelp.class;
rownum=_n_;
newvalue=mod(rownum,6);
run;
Name | Sex | Age | Height | Weight | rownum | newvalue |
Alfred | M | 14 | 69.0 | 112.5 | 1 | 1 |
Alice | F | 13 | 56.5 | 84.0 | 2 | 2 |
Barbara | F | 13 | 65.3 | 98.0 | 3 | 3 |
Carol | F | 14 | 62.8 | 102.5 | 4 | 4 |
Henry | M | 14 | 63.5 | 102.5 | 5 | 5 |
James | M | 12 | 57.3 | 83.0 | 6 | 0 |
Jane | F | 12 | 59.8 | 84.5 | 7 | 1 |
Janet | F | 15 | 62.5 | 112.5 | 8 | 2 |
Jeffrey | M | 13 | 62.5 | 84.0 | 9 | 3 |
John | M | 12 | 59.0 | 99.5 | 10 | 4 |
Joyce | F | 11 | 51.3 | 50.5 | 11 | 5 |
Judy | F | 14 | 64.3 | 90.0 | 12 | 0 |
Louise | F | 12 | 56.3 | 77.0 | 13 | 1 |
Mary | F | 15 | 66.5 | 112.0 | 14 | 2 |
Philip | M | 16 | 72.0 | 150.0 | 15 | 3 |
Robert | M | 12 | 64.8 | 128.0 | 16 | 4 |
Ronald | M | 15 | 67.0 | 133.0 | 17 | 5 |
Thomas | M | 11 | 57.5 | 85.0 | 18 | 0 |
William | M | 15 | 66.5 | 112.0 | 19 | 1 |
So all you need now is to change the 0's to 6's.
Or you can muck around a bit more with the MOD function which ends up something like:
x= mod(i-1, 6)+1;
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.