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

Hello,

 

I would like to create a monotonic() function that would go from 1 to n based on another variable such that if i have a dataset with years, it goes from 1 to x for 2012, 1 to y for 2013, 1 to z for 2014 and so on... is there a way to acheive that? I tried the "group" by function but no luck!

 

Thanks!

 

proc sql; 
create table want as select 
year, 
name, 
score, 
monotonic() as row
from have;
quit; 
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please follow the guidance next to Post button when you start a new question.  Post example test data in the form of a datastep, and show what you want out.  Explain any logic between the two, this avoids us guessing what you want to do.  As @kiranv_ has shown datastep would be simpler for this task, a minor update to use the binary functions:

data want;
  set have;
by year; n=ifn(first.year,1,sum(n,1)); run;

I note all of your questions are about SQL, is there a reason you avoid using Base SAS?  You will find that both have their benefits - SQL for instance does not care about sort on its data as it does it internally, datastep is good at linear steps through data such as this problem.  Unless you connect to a database there really is no need to limit yourself to one or the other.

View solution in original post

3 REPLIES 3
kiranv_
Rhodochrosite | Level 12

Something like this by datastep. if you can show a sample of have and want that would make it easier.

data have;
 input year name $;
 datalines;
 2014 sam
 2014 ram
 2014 kam
 2013 nam
 2013 bam
 2013 lam
 ;
run;

proc sort data =have;
by year name;
run;

data want;
set have;
by year;
if first.year then sum=1;
else sum+1;
run;



 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please follow the guidance next to Post button when you start a new question.  Post example test data in the form of a datastep, and show what you want out.  Explain any logic between the two, this avoids us guessing what you want to do.  As @kiranv_ has shown datastep would be simpler for this task, a minor update to use the binary functions:

data want;
  set have;
by year; n=ifn(first.year,1,sum(n,1)); run;

I note all of your questions are about SQL, is there a reason you avoid using Base SAS?  You will find that both have their benefits - SQL for instance does not care about sort on its data as it does it internally, datastep is good at linear steps through data such as this problem.  Unless you connect to a database there really is no need to limit yourself to one or the other.

camfarrell25
Quartz | Level 8
Thank you! I'll note that for the future. I am more familiar with SQL and the data I use is most often from data warehouse-type sources.

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!

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
  • 3 replies
  • 20836 views
  • 1 like
  • 3 in conversation