BookmarkSubscribeRSS Feed
ballardw
Super User

SAS will not allow character values for numeric variables. So "NA" being letters do not work. However SAS does provide for the display of special text for a missing value by use of a custom display format.

run this:

Proc format library=work;

value Lifeex

 . = 'NA'

;

run;

 

In your data step add the line:

Format Lifeex76 Lifeex. ;

 

Then the value NA will appear for missing values. You will either need to run the Proc Format code every session or dig into the field of pernament libraries, format catalogs and format search paths.

 

You should provide what you want for the EL Salavador data.

It may be you are looking for something like

result = catx('_',substr(scan(name,1),1,3),put(lifeex76,lifeex.));

Reeza
Super User

Ok...basically you need to nest a bunch of functions to get what you want. 

 

Character functions that you need.

 

SUBSTR() -> takes a substring of character variable

CATX() -> concatenates variables, with a specified delimiter

IFN() -> Can be used to check for missing and replace with NA

PUT() -> Convert numeric to character variable

 

I think those 4 will get you want you need in some combination.

You can play around with them to get your result. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I see you guys have been busy overnight!  For my 2p worth I would wrap in a tranwrd() to get the NA - I tend to avoid formats as much as possible.  

data want;
  length result $50;
  infile datalines missover;
  input obs name $ lifeex76;
  result=tranwrd(catx("_",substr(name,1,3),put(lifeex76,best.)),".","NA");
datalines;
1 Algeria 55
2 Angola 
;
run;

You could of course drop the put() statement, however then you would get notes in the log about number to char conversions - I would always put() personally as it is explicit what you are doing there.  Remember coding is not so much about writing fantastically fast and complicated code, but making it simple for the next person to understand what you have coded.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 17 replies
  • 6121 views
  • 0 likes
  • 4 in conversation