DATA Step, Macro, Functions and more

How to get a new variable that iterates by 1

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

How to get a new variable that iterates by 1

Hi,

I have a data set:

LEVEL

4

5

23

64

3

4

I Need:

TIME     LEVEL

1          4

2          5

3          23

4          64

5          3

6          4

How do i read in the "time" variable that iterates by one with each "level" observation?

Thanks in Advance!


Accepted Solutions
Solution
‎05-19-2012 07:48 AM
Respected Advisor
Posts: 3,124

Re: How to get a new variable that iterates by 1

There will be many ways to do that, the simplest way that I can think of:

/*data step*/

data have;

input LEVEL;

cards;

4

5

23

64

3

4

;

data want;

time=_n_;

set have;

run;

/*Proc SQL*/

proc sql;

select monotonic() as time, * from have;

quit;

Haikuo

View solution in original post


All Replies
Solution
‎05-19-2012 07:48 AM
Respected Advisor
Posts: 3,124

Re: How to get a new variable that iterates by 1

There will be many ways to do that, the simplest way that I can think of:

/*data step*/

data have;

input LEVEL;

cards;

4

5

23

64

3

4

;

data want;

time=_n_;

set have;

run;

/*Proc SQL*/

proc sql;

select monotonic() as time, * from have;

quit;

Haikuo

Contributor
Posts: 41

Re: How to get a new variable that iterates by 1

Thanks Haikuo.  That did it!

Super User
Posts: 9,662

Re: How to get a new variable that iterates by 1

I prefer to ODS.

ods _all_ close;
ods output sql_results=want;
proc sql number;
select * from sashelp.class;
quit;
ods listing;

Ksharp

Super Contributor
Posts: 1,636

Re: How to get a new variable that iterates by 1

Hi Ksharp,

what is the advantage to use ODS?

Thanks - Linlin

Respected Advisor
Posts: 3,124

Re: How to get a new variable that iterates by 1

Linlin,

I think what Ksharp mean is that he prefer ODS to monotonic(), not data step. This has be discussed before, and Ksharp was involved. The main reason is that monotonic() is not officially supported by SAS.

Haikuo

Super Contributor
Posts: 1,636

Re: How to get a new variable that iterates by 1

Thank you Haikuo.  - Linlin

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 247 views
  • 0 likes
  • 4 in conversation