Previously posted as a Merge issue, but have now realized it's an Export issue, so staring a new thread:
I'm trying to merge data sets from two different years that identify number of births, by race, by geographic location. For example:
Year1
Location Race # of Births
Location_1 Race_1 0
Location_1 Race_2 1
Location_1 Race_3 2
Location_1 Race_4 3
Location_1 Race_5 0
Year2
Location Race # of Births
Location_1 Race_1 4
Location_1 Race_2 5
Location_1 Race_3 6
Location_1 Race_4 7
Location_1 Race_5 8
Using the following code:
Location Race # of Births Year 1 # of Births Year 2
Location_1 Race_1 . 4
Location_1 Race_2 1 5
Location_1 Race_3 2 6
Location_1 Race_4 3 7
Location_1 Race_5 . 8
Location Race year1 year2
Location_1 Race_1 1 4
Location_1 Race_2 2 5
Location_1 Race_3 3 6
Location_1 Race_4 3 7
Location_1 Race_5 3 8
Through something @Kurt_Bremser suggested on my initial thread here: https://communities.sas.com/t5/SAS-Programming/Merge-duplicating-values-for-what-should-be-null-resp...
I ended up realizing the issue was occurring because SAS was defaulting "0" births as ".", which excel couldn't register as a value. So, I ended up defining a new variable as so:
As I already posted in your other thread, the data in Excel looks exactly like the PROC PRINT output.
Run my code and start from there.
Through something @Kurt_Bremser suggested on my initial thread here: https://communities.sas.com/t5/SAS-Programming/Merge-duplicating-values-for-what-should-be-null-resp...
I ended up realizing the issue was occurring because SAS was defaulting "0" births as ".", which excel couldn't register as a value. So, I ended up defining a new variable as so:
You probably have
options missing=0;
set, which means missing values are always displayed as zeroes, and this "hides" the missing values from the unwary user.
That's why we always present datasets as DATA step code, as that prevents any ambiguities. DATALINES don't lie.
That does not look correct.
That data step looks incorrect.
Is your YEAR1 variable NUMERIC or CHARACTER?
If it is NUMERIC then your IF condition is invalidly comparing a single space character to a number.
If it is CHARACTER then the statement executed by the ELSE is assigning a character value to a NUMERIC variable.
year1 and year 2 are both numeric. I'm not sure why there wasn't an error, or an issue with the if/else statement, but it seemed to work and the excel export had the correct values for each race and year.
@ligbag4 wrote:
year1 and year 2 are both numeric. I'm not sure why there wasn't an error, or an issue with the if/else statement, but it seemed to work and the excel export had the correct values for each race and year.
SAS will try to convert text to numbers or numbers to text , but it should report in the SAS log where it had to do that.
If you want to compare a number to the normal missing value then do that.
if year1 = . then ...
You could also use the MISSING() function which will work on both character and numeric variables. And for numeric variable is will also work for special missing values like .A etc.
if missing(year1) then ...
You could also use the COALESCE() function which would eliminate the need for the IF/THEN logic. Or the need for making a new variable.
year1 = coalesce(year1,0);
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →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.
Ready to level-up your skills? Choose your own adventure.