I want to combine some columns in SAS. Result of That will separated by Enter.
Like
The Result must be in column D. my Expecting Result is:
I need your help on how to provide column A B C to Be D like I showed above.
Many thanks,
You cannot store a key into a variable. You can store character strings. So what character(s) do you want to use? Do you want to add linefeed? ('0A'x) or Carriage Return? ('0D'x) or perhaps both? ('0D0A'x) Or some other character like '|' that you could specify as the SPLIT character so the values will actually display on multiple lines?
data want;
set have ;
length D $50 ;
d = catx('0A'x,a,b,c);
run;
Related
I have a problem with aligning column with 'min, max' and 'q1, q3'. I have predetermined number format and I cannot change it. Five digit number and four digit number cause an error. I would like to have only one space AFTER comma. In column B problem is.
Can I somehow break down column by comma and glue it again?
I have no idea.
I work in SAS.
Thanks in advance!
You can use compbl to remove double spaces in strings:
The COMPBL function removes multiple blanks in a character string by
translating each occurrence of two or more consecutive blanks into a
single blank.
Documentation
B = compbl(B);
You can leave your structure as-is. The code below will remove double spaces from all character variables.
data want;
set have;
array charvars[*] _CHARACTER_;
do i = 1 to dim(charvars);
charvars[i] = compbl(charvars[i]);
end;
drop i;
run;
I have a table that looks similar to this:
A | B
1234|A1B2C
1124|$1n7
1342|*6675
1189|966
I need to create a column C where it takes the data from column B and replaces all non numeric characters with a "9" and makes each one 5 characters long by adding 0's to the front. It should come out like this:
91929
09197
96675
00966
Any assistance would be much appreciated, Thank you!
Edit: Sorry first time posting on any forum like this and got a bit ahead of myself, I created the table using SQL to pull data from 3 other tables and am a bit more familiar with SQL than SAS, which I have only been using for a few weeks. I have tried using COMPRESS but as I read more about that it seem like it only removes the values, so I tried TRANWRD but from what I was able to figure out I would need to create an entry for each letter and symbol that could appear, ie.
data Work.temp;
str = b;
Alpha=tranwrd(str, "a", "9");
Alpha=tranwrd(str, "b", "9");
put Alpha;
run;
so then I researched some more and found SAS replace character in ALL columns
based on that I used this code:
data temp;
set work.temp;
array vars [*] _character_;
do i = 1 to dim(vars);
vars[i] = compress(tranwrd(vars[i],"a","9"));
end;
drop i;
run;
That just returns:
|Str|B|Alpha|
|---.|-.|.-------|
(sorry about the bad formatting there, spent 30 min trying to figure out how to make the table look right with spaces but kept coming out wrong. Please imagine the -'s are spaces)
again any help would be appreciated, Thank you!
try this.
data test;
input var1 $5.;
datalines;
A1B2C
$1n7
*6675
966
;
run;
data test1;
set test;
length var2 $5.;
regex = prxparse ("s/[^0-9|\s]/9/"); /*holds the regular expression you want to use to substitute the non-number characters*/
var2 = prxchange (regex, -1, var1); /*use this function to substitute all instances of the pattern*/
var3 = put (input (var2, best5.), z5.); /*use input and put to pad the front of the variable with 0s*/
run;
Good luck.
Keeping only the digits is simple. Use the modifiers on the COMPRESS() function.
c=compress(b,,'kd');
Padding on the left with zeros there are a number of ways to do that.
You could convert the digits to a number then write it back to a string use the Z format.
c=put(input(c,??5.),Z5.);
You could add the zeros. Using IF statement:
if length(c) < 5 then c=repeat('0',5-length(c)-1)||c ;
Or using SUBSTRN() function.
c=substrn('00000',1,5-length(c))||c;
Or have some fun with the REVERSE() function.
c=reverse(substr(reverse(cats('00000',c)),1,5));
I hope you can help with the below problem.
I have a dataset with a Char column with for example '000000036', 'C', 'M' etc. I want to convert this column to a Numeric column with the examples above displayed as 36, C, M etc.
Thanks
Tope
SAS numeric values can have special missing values, which are referenced in code as .A .B .C etc, but when printed they display as A B C. If your variable only has one character long alpha values, this may be the easiest way to maintain the characters. You can use the MISSING statement to tell SAS which characters represent special missing values.
data have;
input cvar $3.;
cards;
036
C
M
;
missing C M;
data want;
set have;
nvar=input(cvar,3.);
put cvar= nvar=;
run;
missing; *restore default;
If your data has longer character strings, you would need to use a different approach for the conversion (perhaps a custom informat), but I would still consider using special missing values, assuming you have no more than 27 unique character values in the source data.
Numeric columns can only store numbers or missing values. It is possible to display numbers as character, using a format, but that is not relevant here.
#Quentin is correct in that you can use special missing values to display your letters, although as he points out, these are restricted to single letters only. SAS usually stores missing numeric values as a period (.), but you can also use the letters A-Z and the underscore to represent special missing values, these are actually stored as .A, .B, .C,----> ._ .
If you use the missing statement in a data step with all the alphabet letters, then it will automatically assign and display the relevant letter as the special missing value. If any character values are more than one letter then it won't be able to convert or display that value.
data have;
input char_col $15.;
datalines;
000000036
C
M
;
run;
data want;
set have;
missing a b c d e f g h i j k l m n o p q r s t u v w x y z;
num_col=input(char_col,best12.);
run;
i have dataset a
data q7;
input trt$;
cards;
a150
b250
c300
400
abc180
;
run;
We have to create dataset b like this
trt dose
a150 150mg
b250 250mg
c300 300mg
400 400mg
abc180 180mg
new dose variable is added & mg is written after each
numeric values
here is my solution - Basically use the compress functions to keep (hence the 'k') only numbers from the trt variable. From there then is just the case of concatenating mg to numbers.
data want;
set q7;
dose = cats(compress(trt,'0123456789','k'),'mg');
run;
The compress function default behaviour is to return a character string with specified characters removed from the original string.
so
compress(trt,'0123456789') would have removed all numbers from the trt variable.
However compress comes with a battery of modifiers that let the user alter the default behaviour.
So in your case, we wanted to keep numbers regardless of the number of preceding letters so I used the modifier k to keep instead the list of characters in this case 012345679
For a full list of modifiers please read the following link
cats is one of the many functions SAS have to concatenate strings, so passing the compress argument as 1st string and mg as 2nd string will concatenate both to produce your desired result
hope it helps
I have data as follows:
ID date shoesize shoetype
1 4/3/12 . bball
2 . 12 running
3 1/2/12 8 .
4 . 9.5 bball
I want to count the number of '.' there are in each row and make a frequency table with the information. Thanks in advance
You can determine the number of missing values in a row with the NMISS and CMISS functions (NMISS for numeric, CMISS for character). If you have a list of just some of your variables, you should use that list; if not, you need to deal with the fact that number_missing itself will be missing (the -1 there).
data want;
set have;
number_missing=nmiss(of _numeric_) + cmiss(of _character_)-1;
run;
Then do whatever you want with that new variable.
NMISS doesn't work if you wish to evaluate character variables. It converts character variables in the list of arguments to numeric which results in a count being made of missing in every instance that a character variable is encountered. CMISS doesn't convert character variable values to missing and therefore you get the correct answer.
Obviously you can choose not to include the character variables as your arguments, however I am assuming that you want to count missing values in character variables as well, based on the sample you provided. If this is the case the following should do what you want.
DATA WANT3;
SET HAVE;
NUMBER_MISSING = 0;
NUMBER_MISSING=CMISS(OF _ALL_);
RUN;
You must allocate a value to NUMBER_MISSING, otherwise the new variable is also evaluated as a missing.