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

 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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1627176728807.png

 

I'm not sure about your 2nd question. Please provide sample data and desired result. That should explain it.

View solution in original post

4 REPLIES 4
jimbarbour
Meteorite | Level 14

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

CynthiaWei
Obsidian | Level 7

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

Patrick
Opal | Level 21

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;

Patrick_0-1627176728807.png

 

I'm not sure about your 2nd question. Please provide sample data and desired result. That should explain it.

CynthiaWei
Obsidian | Level 7

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1366 views
  • 1 like
  • 3 in conversation