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

I tried self join but its not working.can anyone tell me how to convert it 1 obs per usubjid

 

obs usubjid wt wtu temp tempu

1     100      70  kg

2     100                 34       c

3     101      77  kg

4     101                 30       c

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Urban_Science
Quartz | Level 8

I know you said you tried a self join, but using proc sql I think it should work.  The code would look something like this:

PROC SQL;
  CREATE TABLE WORK.EXAMPLE AS 
     SELECT A.usubjid
        , A.wt
        , A.wtu
        , B.temp
        , B.tempu
      FROM <table> AS A
      INNER JOIN <table> AS B
        ON A.usubjid = B.usubjid
      WHERE A.wt IS NOT NULL
        AND b.TEMP IS NOT NULL
      ;
QUIT;

There might be prettier solutions, but this should get you on your way.  Good luck!

View solution in original post

6 REPLIES 6
Urban_Science
Quartz | Level 8

I know you said you tried a self join, but using proc sql I think it should work.  The code would look something like this:

PROC SQL;
  CREATE TABLE WORK.EXAMPLE AS 
     SELECT A.usubjid
        , A.wt
        , A.wtu
        , B.temp
        , B.tempu
      FROM <table> AS A
      INNER JOIN <table> AS B
        ON A.usubjid = B.usubjid
      WHERE A.wt IS NOT NULL
        AND b.TEMP IS NOT NULL
      ;
QUIT;

There might be prettier solutions, but this should get you on your way.  Good luck!

art297
Opal | Level 21

In a datastep, just use the update statement:

 

data want;
  update have (obs=0) have;
   by usubjid;
run;

Art, CEO, AnalystFinder.com

 

Note: Modified to incorporate @Astounding's correction

 

Astounding
PROC Star

Art's suggestion is a good approach (assuming your data set is sorted), but requires a slight modification:

 

data want;

update have (obs=0) have;

by usubjid;

run;

Urban_Science
Quartz | Level 8

That's a cleaver use of update. Thanks for the other method!

paddyb
Quartz | Level 8

Thanks.Never thought using update its so easy:)

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
  • 6 replies
  • 1254 views
  • 3 likes
  • 4 in conversation