SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
awardell
Obsidian | Level 7

Hello, 

 

I am working on a data set which was provided to me at a cycle level. This means that an individual with the same ID can have more that one visit. I need to create a person level data set in which there is only one occurrence of the participant's ID in the data set. I created derived variables at the cycle level, and now I need to create a person level counterpart. 

 

For example: 

I needed to create a yes/no variable determining if the patient was pregnant in any of the cycles. The person level counterpart would indicate whether the person was ever pregnant. How would you go about turning the yes/no variable into a person level variable? 

 

I was thinking of doing something similar to the following, but it didn't work properly: 

 

data want (drop = preg_counter); 
	set have; 
	by id cycle; 
  	retain preg_counter .;
  if first.id then preg_counter = .;
  if PREG = 1 then do;
    preg_counter = sum(preg_counter,1);
    PREG_COUNT = preg_counter;
  end;
  if PREG_COUNT>=1 then PREG_PERSONLEVEL=1;
run;

to achieve a data set like: 

 

IDCYCLEPregPreg_PERSONLEVEL
1101
1211
1301
2100
2200
311

1

4112
4202
4312

 

Instead, this is produced, which is not helpful: 

IDCYCLEPregPreg_PERSON LEVEL
110.
1211
130.
210.
220.
3111
4111
420.
4312

 

After doing this, I would take the first ID for each patient to retain all of the relevant information in the person level data set. This solution may not be the best way to go about it, so any suggestions or help would be greatly appreciated. 

 

Thank you in advance for all of your help! If there is a solution that avoids the use of SQL, that would be optimal because I am very new to SQL. 

 

Datalines used are as follows: 

data have; 
	input id cycle preg; 
	datalines; 
	1 1 0
	1 2 1
	1 3 0
	2 1 0 
	2 2 0
	3 1 1
	4 1 1
	4 2 0
	4 3 1
; 
run; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Make use of the "auto-remerge" of SAS SQL:

proc sql;
create table want as
  select
    *,
    max(preg) as preg_person_level
  from have
  group by id
;
quit;

or use a data step with a double DO loop:

data want;
preg_person_level = 0;
do until (last.id);
  set have;
  by id;
  if preg then preg_person_level = 1;
end;
do until (last.id);
  set have;
  by id;
  output;
end;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Make use of the "auto-remerge" of SAS SQL:

proc sql;
create table want as
  select
    *,
    max(preg) as preg_person_level
  from have
  group by id
;
quit;

or use a data step with a double DO loop:

data want;
preg_person_level = 0;
do until (last.id);
  set have;
  by id;
  if preg then preg_person_level = 1;
end;
do until (last.id);
  set have;
  by id;
  output;
end;
run;
awardell
Obsidian | Level 7
Thank you so much! I didn't think about it that way!! I appreciate all of your help!
tarheel13
Rhodochrosite | Level 12

If you don't want to use do loops, you can just use set, by, retain.

proc sort data=have;
by id;
run;

data want;
set have;
by id;
retain preg_ever;
if first.id then do;
preg_ever=0;
end;
if preg=1 then preg_ever=1;
if last.id then output;
run;

proc print data=want;
run;

 

I think this gets your desired results.

lrackley_0-1609619785634.png

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1373 views
  • 0 likes
  • 3 in conversation