Greetings All,
I'm running a query in where one set of data has a list of zip codes for members, and I have another file which has a list of zip codes and their corresponding region. The second file is used to map the regions for each zip code. However the first file has some zip codes that do not have a region for, thus when I run the query, I get the '.' value indicating this.
I would like to tell the query that when a match isn't there, instead of giving me '.' , I would rather it fill in the cell with 'NA' instead. I have tried using is null and is missing, but I keep getting a query error, stating that the 'when' statement does not match the requirements.
I am using SAS EG 7.1 and OS Windows 7, thank you.
Hello,
I noticed that you mentioned "Query". I am not familiar with EG 7 environment. However, in case of a SQL, I would recommend using COALESCE function that will exactly do what you need. There should be something similar available on EG 7 or may be the same function is available.... worth looking up...
If you like to see details with examples for COALESCE (), here you go:
https://msdn.microsoft.com/en-us/library/ms190349.aspx
Hope this helps... Good Luck...!!!
@kannand solution is easier :).
And SAS, SAS SQL supports the coalesce function. Process is the same, create a computed column with
Coalesce(var_value, 'NA')
Thank you all for the responses, the Coalesce function did exactly what I needed but I have one other issue, which is what I think what was given me trouble. The variable is considered a number, thus if I try to change it to 'NA' I get an error saying it doesn't match, I'm assuming I have to change my variable into a string. How would I go about doing that, so that I can use the Coalesce to give out an NA value?
Or create a custom format to display missing numeric as NA (or other text);
Proc format;
value Na;
. = 'NA';
run;
And associate that format with the variable. You don't have to change a data type, if you do calculations on the variable it is still treated as missing, and you need not make another pass through the data at all.
I've incorporated the format solution by ballardw in the same example I stated earlier. The format and SQL statement is what you would need to focus.
data have;infile datalines dlm='09'x;
input F_Date:yymmdd8. Analyst_Code;
datalines;
20010305 5
20011228
20020223 7
20020518 5
20020826 7
20021001 9
;RUN;
Proc format;
value Na
. = 'NA';
run;
proc sql;
select f_date,analyst_code format=Na. from have;
Quit;
If using SAS EG and your data is in SAS then you could also create a format with your lookup data (zipcode, region). With EG you can do this in a "point and click" manner using "Tasks/Date/Create format from data set".
Define "N/A" for your "other" cases
Then either create a new variable using "put(zipcode,<format.>)" or simply apply your format directly to the zipcode.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.