SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
cashaowan
Fluorite | Level 6

In my case, I have a diagnosis year defined as yrdxcn and dxyear in character formats in two data sets. Upon merge, I want to select those with same values for yrdxcn and dxyear. Can I just the following?

 

data new; set old;

if yrdxcn = dxyear then output;

run;

 

The log file seemed OK. But the output seemed to tell me that I cannot perform "=" between two character variables because there was 0 observations in the output file. 

 

Thanks.

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

You can use = for your purpose without any issue.  [Edit: without instead of with of course! 😕 ]

 

The reasons seemingly identical strings don't match are:

- Different numbers of leading spaces (trailing spaces do not matter)

- Hidden non-printable characters (such as a tab instead of a space)

- More obviously, case

 

cashaowan
Fluorite | Level 6

I think it is the first reason when I browse the data generated - there were leading spaces for the character variable created by the following. How to getting rid of the leading space when converting numeric variables to character variables?

 

data two; set one;

length dxyear $8.;

dxyear=put(dgyr, 8.);

run;

 

The steps above were used to convert the numeric variable dgyr (length: 8; Format: BEST 12.; Informat: 12.) to the character variable dxyear (length: 8; Format: $8.; Informat: $8.). 

 

Thanks!

ChrisNZ
Tourmaline | Level 20

>  How to getting rid of the leading space when converting numeric variables to character variables?

Many ways. Use functions strip() or left().

Or left justify the format:

data TWO;
  set ONE;
  DXYEAR = put(DGYR, 8. -l);
run;

 

ballardw
Super User

Yes you can. But you are only bringing data from one data set.

data new; 
   set old; /* only the data set old is providing values as input*/
   if yrdxcn = dxyear then output;
run;

The SET statement appends data so you likely need to Merge data. But to make much sense you normally merge on some sort of identification variables.

An example providing some actual data (HINT)

that Merges two data sets on a common identification value and does a comparison as you ask about AND and example of what happens when you use SET with two data sets.

data one;
   input id char $;
datalines;
1  abc
2  xyz
3  pdq
;

data two;
   input id word $;
datalines;
1  abc
2  xyy
3  pdq
4  bbb
;

data example;
   merge one
         two
   ;
   by id;
   if char=word;
run;

data setexample;
  set one
      two
  ;
run;

If you look at the Setexample data set you will see why if you use SET the comparison between Char and Word, using my variables, would never be equal: they do not appear on the same record with values because of the source.

 

To use merge with BY the both data sets will need to be sorted prior to use by the same variable(s) on the By statement.

cashaowan
Fluorite | Level 6
The data old was a successfully merged data set. The data new was comparing two character variables in the merged data set.
ballardw
Super User

@cashaowan wrote:
The data old was a successfully merged data set. The data new was comparing two character variables in the merged data set.

Then SHOW the example data. Include at least some of the values that you think they should be equal.

Your example should be in the form of a data step.

 

A result of 0 observations means that none of the records in the merged data set have the two variables equal. A strong possibility is that the sets, while merged, did not merge in the desired order.

Or that you actually have no matching values for some reason. You do need to show an actual example of values that you think are "equal" that are not creating the desired output.

Better would be to rerun your code and copy from the LOG the entire data step with all the messages or notes that are generated and to paste the lines into a text box opened on the forum with the </> icon. That will preserve the text as created in the log, otherwise the message windows on this forum will reformat the text making some things not appear as they should.

 

 

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2035 views
  • 2 likes
  • 3 in conversation