- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
> 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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.