BookmarkSubscribeRSS Feed
suztang
Fluorite | Level 6

I have two datasets: lifeguards and weather.

The lifeguards set is workshift and name

day name

SunAlice
SunBobby
SunCharlie
FriDanny
FriEllie
SatFran
SatGemma

The weather set is day and high temp

day Temp

Sun39
Mon35
Tue36
Wed36
Thu40
Fri38
Sat39

I want to match merge them so they look like

day name Temp

SunAlice39
SunBobby39
SunCharlie39
Mon.35
Tue.36
Wed.36
Thu.40
FriDanny38
FriEllie38
SatFran39
SatGemma39

 

So I wrote:

data want; merge lifeguards weather;
by day;
run;

But I did not get what I want. I got this instead:

day name Temp

SunAlice.
SunBobby.
SunCharlie.
Sun.39
Mon.35
Tue.36
Wed.36
Thu.40
FriDanny.
FriEllie.
Fri.38
SatFran.
SatGemma.
Sat.39

 

Please help me figure out where I went wrong. I tried it with "in" assignments and using update instead of merge. I don't understand proc sql, so if that's part of your solution, please explain the syntax to me?

6 REPLIES 6
ballardw
Super User

The bit with the Day not aligning likely means that there is actually some difference in the value stored between the lifeguard and weather set. Possible things to look for are leading spaces in one of the sets such as the value is " Sun" in one and the other is "Sun". Another possible issue is that the Format assigned to one of the sets is a $3. format but the length of the values is actually longer. So the value could actually be "Sunday" in one.

How to check if one of these cases may apply.

Run Proc contents on both sets. See if one has a shorter length defined than the other and/or the Format is much shorter than the defined length of the variable.

 

You can check for leading spaces in data step code such as this:

data example;
   x = "   Sun";
   lengthx = length(x);
   y = "Sun";
   lengthy = length(y);
run;

If you print that Example data set above you will see the output doesn't show the leading spaces. Most of the procedures will be display the values without leading spaces.

 

The fix would be to use a data step to make the values in one of the sets look like that of the other. If the issue is leading spaces only then the LEFT function may be appropriate. If the value is longer then the SUBSTR function can be used to shorten the longer values.

 

Does you log show anything about unequal lengths for Day?

 

suztang
Fluorite | Level 6

The real common variable isn't actually a Char., it's a Date8., and it's Date8. in both tables. I am perplexed as this is not my first rodeo with merging and concatenating tables.

Cynthia_sas
SAS Super FREQ

Hi:

  I think you need more control over the output using IN=. You have 2 scenarios in what you want in your final output:

1) rows match in both weather and lifeguard tables

2) have a row in weather, but no lifeguard on that day

3) unlikely -- don't have a row in weather but do have a lifeguard for that day (probable error condition)

 

  At any rate, I only accounted for #1 and #2 in this example using a numeric date variable:

Cynthia_sas_0-1595547950452.png

Hope this helps,

Cynthia

suztang
Fluorite | Level 6

I see what you're doing with that, and it obviously yielded the right table.

 

When I run the same code (variable names changed to protect the innocent), a peculiar thing happens: All the values from my second table get erased in the merge.

ballardw
Super User

@suztang wrote:

The real common variable isn't actually a Char., it's a Date8., and it's Date8. in both tables. I am perplexed as this is not my first rodeo with merging and concatenating tables.


Print some of the date values with a non-date format. If some of the values have decimal portion this is could happen and will not be visible when a date format like DOWNAME3 used. Date8. doesn't display anything resembling Sun, Date8 would display something like 16Mar12 and be right justified in 8 characters making it appear to have a leading space. So why did you show values like Sun????? if that is the format

data example;
   x= 22222.01;
   y= 22222;
   put x= downame3. y= dowwname3. ;
put x= date8. y= date8.;
/* but obviously the values of x and y are different*/ run;

And not telling sufficient detail about your data does not let us help you.

I still say there is something different in the values.

smantha
Lapis Lazuli | Level 10

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 600 views
  • 0 likes
  • 4 in conversation