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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.