Using SAS 9.4
I have many records and I am trying to give each record a number starting with 835 and counting up by 1 from there (836, 837, etc.). I have run the following the code:
proc sort data = controls_merged_back;
by ID;
run;
data step12;
set controls_merged_back;
keep record_id group match_key_final match_key_new2 ID Sex dob side scope_date scope_md Date_of_TKA tka_md obs4;
by ID;
if first.ID then record_id = 835;
mrn +1;
if last.ID then output;
run;
The ID variable I have is unique to each so if my thought was to start with the first ID = to 835 and count by 1 from there. However, when I run this code it produces 835 in each record, so I obviously did something wrong. If anyone has any suggestions that would be great. Thank you
That happens because your original data set already contains RECORD_ID with a value of 0 on every observation. The existing values of 0 throw off the calculations. You can get rid of it at the last possible moment, in simplified form:
data step12;
set controls_merged_back (drop=record_id);
by ID;
retain record_id 834;
if first.ID then record_id + 1;
keep record_id group match_key_final match_key_new2 ID Sex dob side
scope_date scope_md Date_of_TKA tka_md obs4;
run;
@GS2 wrote:
Using SAS 9.4
I have many records and I am trying to give each record a number starting with 835 and counting up by 1 from there (836, 837, etc.). I have run the following the code:
proc sort data = controls_merged_back;
by ID;
run;
data step12;
set controls_merged_back;
keep record_id group match_key_final match_key_new2 ID Sex dob side scope_date scope_md Date_of_TKA tka_md obs4;
by ID;
if first.ID then record_id = 835;
mrn +1;
if last.ID then output;
run;
The ID variable I have is unique to each so if my thought was to start with the first ID = to 835 and count by 1 from there. However, when I run this code it produces 835 in each record, so I obviously did something wrong. If anyone has any suggestions that would be great. Thank you
You are not incrementing RECORD_ID anywhere. So it's always 835.
How about this:
data step12;
set controls_merged_back;
by ID;
if _n_=1 then record_id = 834;
if last.ID then do;
record_id+1;
output;
end;
keep record_id group match_key_final match_key_new2 ID Sex dob side
scope_date scope_md Date_of_TKA tka_md obs4;
run;
That code produces 835 in the first record and 1 in the remaining records. Thank you
That happens because your original data set already contains RECORD_ID with a value of 0 on every observation. The existing values of 0 throw off the calculations. You can get rid of it at the last possible moment, in simplified form:
data step12;
set controls_merged_back (drop=record_id);
by ID;
retain record_id 834;
if first.ID then record_id + 1;
keep record_id group match_key_final match_key_new2 ID Sex dob side
scope_date scope_md Date_of_TKA tka_md obs4;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.