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

Hi,

I am using this command to get a count of distinct ids, but how can I leverage this command to create a dataset with  (all the variables - there are more than 100) based on the distinct ID.

 

proc sql;

create table WANT as

select id, count( distinct id) as count

from HAVE

where qtr=1

AND TEST=0

group by id

;

quit;

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

 

The * in SQL is short for all variables. So you get distinct combinations of the remaining variables as well. Some of those variables apparently have more than one value for a given ID.

 

If you only want distinct values of one variable which value(s) of other variables do you want?

 

 

View solution in original post

7 REPLIES 7
ballardw
Super User

Proc sql;

   create table want as

   select distinct *

   from have;

run;

 

or name the desired variables after the distinct predicate.

 

AZIQ1
Quartz | Level 8
Thank you it did not work.
jklaverstijn
Rhodochrosite | Level 12

@AZIQ1 wrote:
Thank you it did not work.


What happened? Code and log may help.

 

- Jan.

ballardw
Super User

If you have a specific requirement the post the input data and the desired output otherwise there is only guessing.

 

"Doesn't work" is not very helpful. Did it produce no output?Unexpected output? Generate an error? Post log including the code and any notes or error messages.

AZIQ1
Quartz | Level 8

data have;

input id var1 var2 var3 var4 var5 var6 var7 var8 var9 ;

datalines;

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

2 1 1 1 1 1 1 1 1 1

2 1 0 1 0 1 1 0 0 0

2 1 0 1 0 1 1 0 0 0

2 1 0 1 0 1 1 0 0 0

2 1 0 1 0 1 1 0 0 0

3 1 1 1 1 1 1 1 1 1

3 1 1 1 1 1 1 1 1 1

3 1 1 1 1 1 1 1 1 1

3 1 1 1 1 1 1 1 1 1

3 1 1 1 1 1 1 1 1 1

run;

proc sql;

create table want as

select id, count( distinct id) as count

from have

group by id

;

quit;

This gives me 3 distinct count.

 

proc sql;

create table _1 as

select distinct id, *

from have

group by id

;

quit;

 

This gives me 4 ( I need 3 as in the first code)

ballardw
Super User

 

The * in SQL is short for all variables. So you get distinct combinations of the remaining variables as well. Some of those variables apparently have more than one value for a given ID.

 

If you only want distinct values of one variable which value(s) of other variables do you want?

 

 

AZIQ1
Quartz | Level 8
I selected a second var just like you said and it worked.
Thank you

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 24527 views
  • 0 likes
  • 3 in conversation