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

I am a new SAS user , firstly forgive me for my poor English grammar please, then the following is my problem.

I want to write a loop to make the table like that:

 

have:

 

id name

1 Jam

2 John

3 Will

 

want:

 

newid id name

1 1 Jam

2 1 Jam

3 2 John

4 1 Jam

5 2 John

6 3 Will

 

 

 

 

P.S. the logic should be following:

the logic should be when i have N row in raw table;

when I try to create new table, then i write the first row like the first row of raw table; 

then i try to create second step that copy first row and second rows of raw table and they should be second and third rows in new table;

then when i create third step that copy first,second and third rows of raw table and  they shoud be forth,fifth and sixth rows in new table;

etc

 

the Nth step should be that copy first row to Nth rows of raw table, then the whole number of rows should be (1+N)*(N/2), the first row of raw table is copied in N times and second one should be (N-1) times; 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Reading the input only once:

 

data have;
input id name $;
datalines;
1 Jam
2 John
3 Will
;

data want;
set have nobs=nobs;
do i = _n_ to nobs;
    newId = (i * i-1)/2 + _n_;
    output;
    end;
drop i;
run;

proc sort data=want; by newId; run;

proc print; run;
PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

Reading the input only once:

 

data have;
input id name $;
datalines;
1 Jam
2 John
3 Will
;

data want;
set have nobs=nobs;
do i = _n_ to nobs;
    newId = (i * i-1)/2 + _n_;
    output;
    end;
drop i;
run;

proc sort data=want; by newId; run;

proc print; run;
PG
Reeza
Super User

Whats the logic? You can control the output of a line using the OUTPUT statement, but it depends on the logic.

For example, this outputs the first two rows of the sashelp.class data set the number of times of the variable age.

 

data want;
set sashelp.class (obs=2);

do i=1 to age;
      output;
end;

drop i;
run;

 


@Howiewang wrote:

I am a new SAS user , firstly forgive me for my poor English grammar please, then the following is my problem.

I want to write a loop to make the table like that:

 

have:

 

id name

1 Jam

2 John

3 Will

 

want:

 

newid id name

1 1 Jam

2 1 Jam

3 2 John

4 1 Jam

5 2 John

6 3 Will



 

Howiewang
Calcite | Level 5

the logic should be when i have N row in raw table;

when I try to create new table, then i write the first row like the first row of raw table; 

then i try to create second step that copy first row and second rows of raw table and they should be second and third rows in new table;

then when i create third step that copy first,second and third rows of raw table and  they shoud be forth,fifth and sixth rows in new table;

etc

 

the Nth step should be that copy first row to Nth rows of raw table, then the whole number of rows should be (1+N)*(N/2), the first row of raw table is copied in N times and second one should be (N-1) times; 

 

that's the logic what i want,

thank for your patience to read this description.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2088 views
  • 2 likes
  • 3 in conversation