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-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 988 views
  • 1 like
  • 3 in conversation