SAS Pros,
I am having a question that I want to check whether the two variables are corresponding to each other, which means if the value of 100 in variable1 corresponds to 10000 in variable 2, then that 100 will not correspond to other values in variable 2. The tricky thing is that when sorting variable 1 in an either ascending or descending way, variable 2 will be disorderly and may not continuous.
Another questions is that I may have two or more observations under each ID, and each observation may have a different date. I want to whether the dates among the two or more observations under each ID are the same?
Thank you for any help!
Best regards,
C
Providing sample data via a SAS data step and then showing the desired result often helps tremendously in understanding the question.
Below an approach to answer your first question the way I understand it.
data sample;
do id=1 to 1000;
var1=id;
var2=id*10;
output;
end;
var1+1;
output;
var2+1;
output;
run;
title "var2 value with more than one corresponding var1 value";
proc sql;
select
var2,
count(distinct var1) as n_values
from sample
group by var2
having n_values ne 1
;
quit;
title;
title "var1 value with more than one corresponding var2 value";
proc sql;
select
var1,
count(distinct var2) as n_values
from sample
group by var1
having n_values ne 1
;
quit;
title;
I'm not sure about your 2nd question. Please provide sample data and desired result. That should explain it.
Hi, @CynthiaWei,
I'm having a little trouble visualizing this. Can you provide a sample of the data that you have and an example of the results you'd like to see?
Jim
Thank you JimBarbour all the same for checking in with me! I am sorry for the confusion. As I replied to Patrick, both my questions are solved.
Best regards,
C
Providing sample data via a SAS data step and then showing the desired result often helps tremendously in understanding the question.
Below an approach to answer your first question the way I understand it.
data sample;
do id=1 to 1000;
var1=id;
var2=id*10;
output;
end;
var1+1;
output;
var2+1;
output;
run;
title "var2 value with more than one corresponding var1 value";
proc sql;
select
var2,
count(distinct var1) as n_values
from sample
group by var2
having n_values ne 1
;
quit;
title;
title "var1 value with more than one corresponding var2 value";
proc sql;
select
var1,
count(distinct var2) as n_values
from sample
group by var1
having n_values ne 1
;
quit;
title;
I'm not sure about your 2nd question. Please provide sample data and desired result. That should explain it.
Hi Patrick,
Thank you so much for your codes!
Actually, I can apply your following codes to my 2nd question. It is still about checking whether same ID corresponds to multiple dates.
It is very helpful!
Best regards,
C
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.