Hi,
Taking a guess at your data (as you have not put test data in the form of a datastep here so that we have something to work with):
data want; charvar="XYZDEFGH"; numvar=43256; result=catx("_",substr(charvar,1,3),put(numvar,best.)); run;
Substr is the function to get the characters you want, put() is the function to show numbers as text, and catx concatenates text with a delimiter, in this case "_".
Right of an = is the value, left is the name of the variable.
So the statement
charvar="XYZDEFGH";
Creates a varaible named Charvar and assigns the value of XYZDEFGH.
What will your rule be if the name only has 1 or 2 characters? Do you need _ to make a fixed length or will 1 or 2 characters followed by a single _ suffice?
@smashing If you have an answer please mark the question solved. If you still need help please post some samples of what your observation looks like and what you want as output.
There are probably several methods to do this so if you want alternate solutions you should post sample data anyways.
You didn't post what you wanted as output, but a CATT should work.
want = CATT(substr(name, 1, 3), lifeex76);
CATT converts numerics to character and SUBSTR takes the first 3 characters of the name.
Hey,
That almost works but I need an underscore between the two values and adding a , '_' , doesn't seem to work. It says substr has too many arguments. Any suggestions?
Thanks!
This is why you show the output you want...
CATX instead.
CATX("_", substr..., othervariable)
Your data has missing values for lifeex76 for observations 8, 11, 14, 16 and others. What is the rule for those? Your current rule would possibly yield something like Cap_. for Cape Verde.
Also your observation 56 would yield "El _58" with a space betwee the "l" and the "_". Is that going to be the desired result?
You may want to provide some examples of how the constructed variable is to be used to get better suggestions.
Incidentially, my attempt to replace . with NA doesn't seem to work--getting this outpu:
name=Guinea-Bissau lifeex76=. urpop76=20.8 indeat70=173 _ERROR_=1 _N_=21
NOTE: Invalid numeric data, 'NA' , at line 175 column 32.
name=Swaziland lifeex76=. urpop76=14 indeat70=147 _ERROR_=1 _N_=43
NOTE: Invalid numeric data, 'NA' , at line 175 column 32.
name=Barbados lifeex76=. urpop76=38.6 indeat70=33 _ERROR_=1 _N_=51
NOTE: Invalid numeric data, 'NA' , at line 175 column 32.
name=Guyana lifeex76=. urpop76=29.6 indeat70=56 _ERROR_=1 _N_=72
NOTE: Invalid numeric data, 'NA' , at line 175 column 32.
name=Suriname lifeex76=. urpop76=44.8 indeat70=55 _ERROR_=1 _N_=75
Any suggestions?
Thanks!
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.));
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: