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

Can someone tell me what I'm doing wrong here?

 

 

data one;

input persons $ city $ rank;

datalines;

twoperson dc 29

twoperson la 14

twoperson nyc 17 ;

run;

 

 

data two;

input persons $ city $ rank;

datalines;

threeperson dc 32

threeperson la 13

threeperson nyc 64 ;

run;

 

 

proc sort data=one;

by city;

proc sort data=two;

by city;

run;

 

data combined;

merge one two;

by city;

run;

proc print data=combined;

run;

 

Right now it's just reprinting data two.

I need it to look like this:

 

       twoperson     threeperson 

dc        29                     32

la           14                     13

nyc        17                      64

 

 

so what am I doing wrong SAS board?

any and all help much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

Hi, try ...

 

data three (drop=persons);
merge one (rename=(rank=twoperson)) two (rename=(rank=threeperson));
by city;
run;

 

Obs    city    twoperson    threeperson

 1     dc          29            32
 2     la          14            13
 3     nyc         17            64

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Merge does exactly what you see it doing.

 

In order to turn the rows into columns named twoperson and threeperson, you would have to run PROC TRANSPOSE on your data sets one and two (which turns rows into columns), and then merge.

 

Also, you don't want a semicolon at the end of your "nyc" datalines.

--
Paige Miller
arodriguez
Lapis Lazuli | Level 10

Hi nursienurse,

Merge statement join two datasets by a common variable.

Right now, you are joining by city, so you are creating a dataset with variables persons, city and rank, but mixing info of two datasets.

Try to define dataset two with an other column name, something like 

data two;
input persons2 $ city $ rank2;
datalines;
threeperson dc 32
threeperson la 13
threeperson nyc 64 ;
run;

(Take care of names are persons2, rank2 now)

So when you merge now, you should optain something like

       persons       persons2       rank                  rank2    

dc    twoperson  threeperson   29                     32

la     twoperson  threeperson   14                     13

nyc  twoperson  threeperson   17                      64

MikeZdeb
Rhodochrosite | Level 12

Hi, try ...

 

data three (drop=persons);
merge one (rename=(rank=twoperson)) two (rename=(rank=threeperson));
by city;
run;

 

Obs    city    twoperson    threeperson

 1     dc          29            32
 2     la          14            13
 3     nyc         17            64

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