label a variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

label a variable

Hi,

I have a sorted variable date. I want to label each date from 1 to N. Please see below

date               new variable N

01/01/1990     1

01/01/1990     1

01/01/1990     1

02/01/1990     2

02/01/1990     2

03/01/1990     3

...                    ...

How to create this N? Also, if there is another variable, say fund id, how can I create new N by the fund id?

Thanks.


Accepted Solutions
Solution
‎07-15-2013 11:01 PM
Super User
Super User
Posts: 6,500

Re: label a variable

If the only variable you want to use for numbering the groups is DATE then change the code to only use that variable.

data want ;

  set have ;

  by date;

  if first.date then n+1;

run;

If you are having trouble understanding how FIRST. and LAST. works then create some sample data and play with it to see what is happening.

data _null_;

   set have;

   by date ;

   put (_n_ date first.date last.date) (=);

run;

View solution in original post


All Replies
Super Contributor
Posts: 297

Re: label a variable

Hi SeanZ,

I don't understand. Do you want to add labels to the variables DATE, NEW VARIABLE and N or do you want to add a value in N which relates to DATE in someway? 

What do you want the output to look like?

Regards,

Scott

Contributor
Posts: 26

Re: label a variable

you can use first.date and add a new count variable and increment it.

Frequent Contributor
Posts: 122

Re: label a variable

Thanks, Scott. Tom gave the perfect solution to my question.

Frequent Contributor
Posts: 122

Re: label a variable

I want to create a new variable N like the ones in my question.

Super User
Super User
Posts: 6,500

Re: label a variable

You can use BY processing. You will want to retain the new variable.  Set to 1 (or 0) on new ID and increment on new date.

data want ;

  set have ;

  by id date;

retain n;

  if first.id then n=0;

  if first.date then n=n+1;

run;

Frequent Contributor
Posts: 122

Re: label a variable

Hi Tom,

It seems that this doesn't work in my case. I only need to count the date, without id at this moment. I tweaked your code as below

data want ;

  set have ;

  by id date;

retain n;

if first.date then n=0;

n+1;

run;

It results in the variable n = 1 to ... until the last unchanged date, and n=1 start from the first changed date and repeat. In my first example. The result will be

date               new variable N          I want N to be

01/01/1990     1                                   1

01/01/1990     2                                   1

01/01/1990     3                                   1

02/01/1990     1                                   2

02/01/1990     2                                   2

03/01/1990     1                                   3

...                    ...

Please help. Thanks.

Solution
‎07-15-2013 11:01 PM
Super User
Super User
Posts: 6,500

Re: label a variable

If the only variable you want to use for numbering the groups is DATE then change the code to only use that variable.

data want ;

  set have ;

  by date;

  if first.date then n+1;

run;

If you are having trouble understanding how FIRST. and LAST. works then create some sample data and play with it to see what is happening.

data _null_;

   set have;

   by date ;

   put (_n_ date first.date last.date) (=);

run;

Frequent Contributor
Posts: 122

Re: label a variable

Thanks so much. That benefits a lot.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 349 views
  • 2 likes
  • 4 in conversation