Replacing a Character variable with part of another Character variable #3Good evening all,
I have a small problem that I need to solve and would appreciate any
help you offer.
I created the below table using some raw data. I created the
Location
variable using the
ZIPCITY function. As you can see, in the last observation the City
"Harrisbu" does not match the city under Location "Levittown" while
for the first observation the Location is left blank because the
zipcode is invalid. I need a simple code that does the following:
1.If they zipcode does not match the city/state (6th observation),
create a code that suggests either edits to the zipcode or city/
state.
One way is is to compare the "City" variable with the first part
(city part) of the "Location" varaible and asking SAS to replace the
former
with the latter if they don't match (e.g replace Harrisbu with
Levittown). Needless to say, I don't now
how to do that...
2. If the Zipcode is invalid (1st observation) i need to replace its
value with "???"
This is just part of the dataset so I need something that is global.
Also, please keep it as simple as possible so that I can understand
what you did. Thanks a lot !
City zipcode State StateName location
Buffalo 10068 NY New York
Isabel 67065 KS Kansas Isabel, KS
Lansing 48910 MI Michigan Lansing, MI
Lithonia 30058 GA Georgia Lithonia, GA
Greenbay 54311 WI Wisconsin Green Bay, WI
Harrisbu 19057 PA Pennsylvania Levittown, PA
...
Replacing a Character variable with part of another Character variable #2Good evening all,
I have a small problem that I need to solve and would appreciate any
help you offer.
I created the below table using some raw data. I created the
Location
variable using the
ZIPCITY function. As you can see, in the last observation the City
"Harrisbu" does not match the city under Location "Levittown" while
for the first observation the Location is left blank because the
zipcode is invalid. I need a simple code that does the following:
1.If they zipcode does not match the city/state (6th observation),
create a code that suggests either edits to the zipcode or city/
state.
One way is is to compare the "City" variable with the first part
(city)
of the "Location" varaible and asking SAS to replace the former
with the latter if they don't match. Needless to say, I don't now
how to do that...
2. If the Zipcode is invalid (1st observation) i need to replace its
value with "???"
This is just part of the dataset so I need something that is global.
Also, please keep it as simple as possible so that I can understand
what you did. Thanks a lot !
Obs PatientName City StateName
location zipcode
1 Sonya Larson Buffalo New
York 10068
2 Peter Simpson Isabel Kansas Isabel,
KS 67065
3 Kip Holfser Lansing Michigan Lansing,
MI 48910
4 Chan Rong ...
Re: Replacing a Character variable with part of another Character> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of SAS
> Student
> Sent: Saturday, December 08, 2007 2:24 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Replacing a Character variable with part of another Character variable
>
> Good evening all,
>
> I have a small problem that I need to solve and would appreciate any
> help you offer.
> I created the below table using some raw data. I created the Location
> variable using the
> ZIPCITY function. As you can see, in the last observation the City
> "Harrisbu" does not match the city under Location "Levittown" while
> for the first observation the Location is left blank because the
> zipcode is invalid.
It will be a lot easier for people to help you if you provide a self-contained program that generates your sample data, rather than making them try to extract data from a poorly formatted email (everything is wrapping haphazardly at my end of the email).
That being said, is this just a toy problem or is this a real problem you are trying to solve. I ask, because ZIP codes are not geographical regions, but rather mail delivery routes. There are many places where the zipcode "location" does not match the town. In fact, sometimes even the state won't match. So I'm not convinced you should be editing anything.
>I need a simple code that does the following:
>
> 1.If they zipcode does n...
Re: Replacing a Character variable with part of another Character #2On Sat, 8 Dec 2007 14:58:19 -0800, Daniel Nordlund <res90sx5@VERIZON.NET> wrote:
>> -----Original Message-----
>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of SAS
>> Student
>> Sent: Saturday, December 08, 2007 2:24 PM
>> To: SAS-L@LISTSERV.UGA.EDU
>> Subject: Replacing a Character variable with part of another Character
variable
>>
>> Good evening all,
>>
>> I have a small problem that I need to solve and would appreciate any
>> help you offer.
>> I created the below table using some raw data. I created the Location
>> variable using the
>> ZIPCITY function. As you can see, in the last observation the City
>> "Harrisbu" does not match the city under Location "Levittown" while
>> for the first observation the Location is left blank because the
>> zipcode is invalid.
>
>It will be a lot easier for people to help you if you provide a
self-contained program that generates your sample data, rather than making
them try to extract data from a poorly formatted email (everything is
wrapping haphazardly at my end of the email).
http://www.puzha.com/sasbook/sas%20examples.html#_Toc145244126
>
>That being said, is this just a toy problem or is this a real problem you
are trying to solve. I ask, because ZIP codes are not geographical regions,
but rather mail delivery routes. There are many places where the zipcode
&quo...
how to sort variable by character variable first then numeric variableHi,
who knows if we can use SAS to sort the variables by character
variable first and then numeric variable.
Such as:
The original order is:
StudentID Score Gender Address
I want to sort it as
StudentID Gender Address Score
Any hint?
Thanks very much!
Check these sites for guidance on using proc sort and using proc sql.
Look for the by statement in proc sort and the order by statement in
proc sql.
Good Luck.
AB3BO
Don
http://www.ats.ucla.edu/STAT/sas/modules/sort.htm
http://www2.sas.com/proceedings/sugi27/p191-27.pdf
On Apr 26, 12:00 pm, Karen <irain...@gmail.com> wrote:
> Hi,
>
> who knows if we can use SAS to sort the variables by character
> variable first and then numeric variable.
>
> Such as:
> The original order is:
> StudentID Score Gender Address
>
> I want to sort it as
>
> StudentID Gender Address Score
>
> Any hint?
>
> Thanks very much!
...
How to add character in between the character variableHello,
I have a character variable named rsABCD with the VALUE "GG".
I wantr to make it "G/G". How to do it....I
Hello,
I have a character variable named rsABCD with the VALUE "GG".
I want to make it "G/G". How to do it....I
Infact I have hundreds of variables in the dataset similar to rsABCD
and I want All of them to appear as "G/G" or "A/T"
...
Calling a variable by a character variableIt is better using an example.
I would call a variable by keyword and I would see his value on my
screen.
More or less:
CHARACTER :: var_name
myvar1 = 5
myvar2 = 7
.......
myvar1000 = 8
Write(*,*) 'variable name'
Read(*,*) var_name
I would type 'myvar1' (or mayvar2, myvar3,...) and then I would see
the respective variable's value on my screen.
I have a lot of variables, therefore a solution like
If var_name='myvar1' write(*,*) myvar1
etc..
could be not convenient.
How can I do?
Thanks and I am sorry I was not clear
Allamarein wrote:
>...
Change character variable to num variableHi, all
How can I change this character variable "2007/02" ($7.) to a numeric
one?
Thanks
Data _Null_ ;
OldDate = '2007/02' ;
NewDate = MDY( InPut( Scan( OldDate , 2 , '/' ) , 8. ) ,
1 ,
InPut( Scan( OldDate , 1 , '/' ) , 8. ) ) ;
Put OldDate= NewDate= Date9. ;
Run
Toby Dunn
If anything simply cannot go wrong, it will anyway. Murphys Law #2.
The buddy system is essential to your survival; it gives the enemy somebody
else to shoot at.
Murphys Law #
Tell a man there are 300 billion stars in the universe and he'll b...
Re: How to add character in between the character variableLook at the example:
data a;
length rsABCD $100;
rsABCD = "ölksdfdslök AThjsaGGdaATasGGklsf";
put rsABCD=;
rsABCD = tranwrd(rsABCD,"GG","G/G");
rsABCD = tranwrd(rsABCD,"AT","A/T");
put rsABCD=;
run;
You might have a problem if rsABCD is not long enough. In that case you
must define another char variable which is long enough and assign the
result to that new var.
Gerhard
On Wed, 4 Mar 2009 05:00:17 -0800, vrajeshrawal@gmail.com
<vrajeshrawal@GMAIL.COM> wrote:
>Hello,
>
>I have a character variable named rsABCD with the VALUE "GG".
>
>I want to make it "G/G". How to do it....I
>
>Infact I have hundreds of variables in the dataset similar to rsABCD
>and I want All of them to appear as "G/G" or "A/T"
How about something more generic:
data old ;
input rsABCD $ ;
cards ;
GG
AT
XY
ABC
;
data new ; set old ;
newrsABCD = catx( "/", substrn(rsABCD,1,1), substrn(rsABCD,2,1) ) ;
run ;
...
Convert Character Variable to Integer VariableHow do you convert a character variable to an integer variable?
variable my_char_v : character;
variable file_size : integer;
.. . .
file_size := ???? my_char_v;
Brad Smallridge wrote:
> How do you convert a character variable to an integer variable?
> variable my_char_v : character;
> variable file_size : integer;
>
> file_size := ???? my_char_v;
file_size := character'pos(my_char_v);
-- Mike Treseler
> file_size := character'pos(my_char_v);
Yeah that worked. Now one would think that'd
only work to convert to a character.
...
how to change the character variable to a time variable ?For a character variable "time"(its form as "103839", and
denote: am. 10 o'closk 38 minute 39 second ). I want to know how to
change the character variable to a time variable and in order that I
can pick the second"39"?
Thanks!
ljmpll@sohu.com wrote:
> For a character variable "time"(its form as "103839", and
> denote: am. 10 o'closk 38 minute 39 second ). I want to know how to
> change the character variable to a time variable and in order that I
> can pick the second"39"?
> Thanks!
Ljmpll,
What does y...
how to change the character variable to numerical variable?Hi,
I am trying to use the function
num_variable=input(char_variable,BEST12.) to change the variable type
from char to num. How to do that in the program?
Thanks so much!
...
Re: How to add character in between the character variable #9If that's what he or she meant, why didn't he or she just say so in the
first place? I want to be able to assume that a question contains the
information we need to answer it.
On Wed, 4 Mar 2009 12:04:59 -0600, "Mary" <mlhoward@AVALON.NET> said:
> Given that the questioner's variable is named "rs...", and he
> sites that he has an example of GG, I assume this is a genetic
> SNP since most SNPs start with rs in the national database.
> See:
> http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?searchType=adhoc_search&type=rs&rs=rs2072633
>
> as an example of how the NIH database works.
>
> Thus, yes, the questioner probably has a two character variable
> that he wants to put a slash between the two characters- in particular,
> some of the SAS Genetics procs like Proc Allele require that the
> data variable look like A/G.
>
> -Mary
> ----- Original Message -----
> From: Gerhard Hellriegel
> To: SAS-L@LISTSERV.UGA.EDU
> Sent: Wednesday, March 04, 2009 11:56 AM
> Subject: Re: How to add character in between the character variable
>
>
> What does that mean "genetic" data? Are genetic data always pairs of
> characters and you want to fit a char between all that pairs? Or longer
> strings and you want to put a '/' or whatever at the second, the fifth
> and
> the ninth position in each string? Or between all pairs of characters,...
Re: How to add character in between the character variable #5It isn't going to be just a GG; this is genetic data, thus it would be =
likely to be=20
an AA, AG, and GG in that variable, and different letters in each =
variable,
hence the substring approach.=20
-Mary
----- Original Message -----=20
From: Gerhard Hellriegel=20
To: SAS-L@LISTSERV.UGA.EDU=20
Sent: Wednesday, March 04, 2009 10:37 AM
Subject: Re: How to add character in between the character variable
not a good idea, I think. What about
abcGGvsfeabsgGGbsbsbATbdbdbdb ???
data old ;
input rsABCD : $50. ;
cards ;
abcsdefGGvavavav
____AT____
####abababXY_gdgdgd
bdbdbd ABC gdgdgdg
;
data new ; set old ;
newrsABCD =3D catx( "/", substrn(rsABCD,1,1), substrn(rsABCD,2,1) ) =
;
run ;
what do you have against
newrsABCD =3D tranwrd(rsABCD,"GG","G/G");
?
Gerhard
On Wed, 4 Mar 2009 07:59:40 -0800, Haris <Karovaldas@GMAIL.COM> wrote:
>How about something more generic:
>
>data old ;
> input rsABCD $ ;
> cards ;
>GG
>AT
>XY
>ABC
>;
>
>data new ; set old ;
> newrsABCD =3D catx( "/", substrn(rsABCD,1,1), substrn(rsABCD,2,1) =
) ;
>run ;
...
Re: How to add character in between the character variable #10On Wed, Mar 4, 2009 at 6:30 PM, vrajeshrawal@gmail.com
<vrajeshrawal@gmail.com> wrote:
> Hello,
>
> I have a character variable named rsABCD with the VALUE "GG".
>
> I want to make it "G/G". How to do it....I
>
> Infact I have hundreds of variables in the dataset similar to rsABCD
> and I want All of them to appear as "G/G" or "A/T"
>
Also, if you have multiple variables which you want to transform, then
don't forget to put them all in an array and iterate through the array
-
data xyz;
rsABCD = "GG"; output;
rsEFGH = "AT", output;
run;
data xyz2;
set xyz;
array rsvars{*} rs:
do rscount = 1 to dim(rsvars);
/* Put code suggested by others with the variable as rs{rscount} */
end;
run;
Regards,
Anindya
...
Re: How to add character in between the character variable #7That's why I generally avoid trying to answer poorly-defined questions.
I don't understand this great reluctance on the part of many posters,
especially newcomers, to explain what it is they want to do - why make
the rest of us guess?
On Wed, 4 Mar 2009 12:56:25 -0500, "Gerhard Hellriegel"
<gerhard.hellriegel@T-ONLINE.DE> said:
> What does that mean "genetic" data? Are genetic data always pairs of
> characters and you want to fit a char between all that pairs? Or longer
> strings and you want to put a '/' or whatever at the second, the fifth
> and
> the ninth position in each string? Or between all pairs of characters, so
> all of them are seperated after the operation, or ...?
> My idea was, that there are special pairs of characters which should all
> be replaced by a separated triple. That is not possible with "hardcoded"
> positions, if I don't know where the pairs are located.
> Something must be clear about the rules how to do and what to do. It is
> not possible to change that rules for each obs, only if you do that
> manually. If that rule is "change AA, AG and GG in ALL obs, no problem.
> If
> that rule is "insert a certain char at certain positions for all obs", no
> problem. If the rule is, "...do that only for odd numbers of obs", still
> no problem. That rule can be as complicated as you want, no problem. But
> if you say, there ...
Re: How to add character in between the character variable #3not a good idea, I think. What about
abcGGvsfeabsgGGbsbsbATbdbdbdb ???
data old ;
input rsABCD : $50. ;
cards ;
abcsdefGGvavavav
____AT____
####abababXY_gdgdgd
bdbdbd ABC gdgdgdg
;
data new ; set old ;
newrsABCD = catx( "/", substrn(rsABCD,1,1), substrn(rsABCD,2,1) ) ;
run ;
what do you have against
newrsABCD = tranwrd(rsABCD,"GG","G/G");
?
Gerhard
On Wed, 4 Mar 2009 07:59:40 -0800, Haris <Karovaldas@GMAIL.COM> wrote:
>How about something more generic:
>
>data old ;
> input rsABCD $ ;
> cards ;
>GG
>AT
>XY
>ABC
>;
>
>data new ; set old ;
> newrsABCD = catx( "/", substrn(rsABCD,1,1), substrn(rsABCD,2,1) ) ;
>run ;
...
code to convert a character variable to numeric variable withI have been struggling with this code that keeps giving me a nasty warning message that says "it cannot perform mathematical operation blah blah blah"? when I think I have followed all the syntax rules on coding a SAS program.
Here's the picture:
input variable is HRMIN $5, and the value of HRMIN variable looks like this 11:45.
my client wants this output as?a numeric variable called ?OUTHRMIN with?time5. format.
my code?within my datastep is:? OUTHRMIN = input(HRMIN,time5.);
and I get these error (actually, note:)?messages:
NOTE: Invalid argument to function INPUT at line 1327 column 20.
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to
missing values.
Each place is given by: (Number of times) at (Line):(Column).
2 at 1327:20
Could someone see any glaring error in my code?
??
lost in TIME,
Meenit
________________________________________________________________________
More new features than ever. Check out the new AOL Mail ! - http://webmail.aol.com
...
Re: How to add character in between the character variable #4What does that mean "genetic" data? Are genetic data always pairs of
characters and you want to fit a char between all that pairs? Or longer
strings and you want to put a '/' or whatever at the second, the fifth and
the ninth position in each string? Or between all pairs of characters, so
all of them are seperated after the operation, or ...?
My idea was, that there are special pairs of characters which should all
be replaced by a separated triple. That is not possible with "hardcoded"
positions, if I don't know where the pairs are located.
Something must be clear about the rules how to do and what to do. It is
not possible to change that rules for each obs, only if you do that
manually. If that rule is "change AA, AG and GG in ALL obs, no problem. If
that rule is "insert a certain char at certain positions for all obs", no
problem. If the rule is, "...do that only for odd numbers of obs", still
no problem. That rule can be as complicated as you want, no problem. But
if you say, there are no rules, it depends on, I think it's very hard to
code.
Gerhard
On Wed, 4 Mar 2009 11:35:49 -0600, Mary <mlhoward@AVALON.NET> wrote:
>It isn't going to be just a GG; this is genetic data, thus it would be
likely to be
>an AA, AG, and GG in that variable, and different letters in each
variable,
>hence the substring approach.
>
>-Mary
> ----- Original Message -----
> From: Gerhard Hellriegel
> ...
Re: How to add character in between the character variable #2informat rsabcd_c $3.;
rsabcd_c=3D substr(rsabcd,1,1) || '/' || substr(rsabcd,2,1);
-Mary
----- Original Message -----=20
From: vrajeshrawal@gmail.com=20
To: SAS-L@LISTSERV.UGA.EDU=20
Sent: Wednesday, March 04, 2009 6:15 AM
Subject: How to add character in between the character variable
Hello,
I have a character variable named rsABCD with the VALUE "GG".
I wantr to make it "G/G". How to do it....I
...
Re: how to change the character variable to numerical variable?You just need a data step and fill in using variable names that apply
to your data.
data test;
a = '123.4';
b = input(a,8.);
run;
proc contents;
run;
Keep in mind that you have to create a "new" numeric variable. That
is you cannot "convert" A in the example above to numeric.
On Sun, Mar 2, 2008 at 12:28 AM, irenelj23@gmail.com
<irenelj23@gmail.com> wrote:
> Hi,
> I am trying to use the function
> num_variable=input(char_variable,BEST12.) to change the variable type
> from char to num. How to do that in the program?
> Thanks so much!
>
Thanks so much!
There is a variable in dataset1 in the exercise library. I tried to
change the type of one of the variables-ID from character to
numerical. Is this the right way to do?
I first created a new variable id_num and then dropped the old
variable ID which is character, then I changed the name of the
numerical id_num to the original name ID.
data exercise.dataset1;
set exercise.dataset1;
id_num=input(ID,best12.);
drop sampid;
rename sampid_num=sampid;
Thanks for your help!
...
Re: how to convert character variable to a date variablealberthz@STANFORD.EDU wrote:
>I have a variable like:
>20010101
>20010102
>20010103
>20020101
>......
>
>
>it's date in fact, but when I import it,the SAS recognize it as character
>variable.
>
>
>How can I convert it to a date variable or numerical variable?
It depends.
[1] How are you 'importing' it? By 'import', do you mean that you're
using PROC IMPORT? Or are you using the Import wizard? Or are you using
a DATA step to read the file?
[2] What are you importing from? If it is being iported from Excel or
Oracle or
MS SQL ...
Re: How to add character in between the character variable #8Given that the questioner's variable is named "rs...", and he
sites that he has an example of GG, I assume this is a genetic
SNP since most SNPs start with rs in the national database.
See:
http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?searchType=3Dadhoc_search&typ=
e=3Drs&rs=3Drs2072633
as an example of how the NIH database works.
Thus, yes, the questioner probably has a two character variable
that he wants to put a slash between the two characters- in particular,
some of the SAS Genetics procs like Proc Allele require that the=20
data variable look like A/G.
-Mary
----- Original Message -----=20
From: Gerhard Hellriegel=20
To: SAS-L@LISTSERV.UGA.EDU=20
Sent: Wednesday, March 04, 2009 11:56 AM
Subject: Re: How to add character in between the character variable
What does that mean "genetic" data? Are genetic data always pairs of
characters and you want to fit a char between all that pairs? Or =
longer
strings and you want to put a '/' or whatever at the second, the fifth =
and
the ninth position in each string? Or between all pairs of characters, =
so
all of them are seperated after the operation, or ...?
My idea was, that there are special pairs of characters which should =
all
be replaced by a separated triple. That is not possible with =
"hardcoded"
positions, if I don't know where the pairs are located.
Something must be clear about the rules how to do and what to do. It =
is
not possib...
Re: Change character variable to num variableData _Null_ ;
OldDate = '2007/02' ;
NewDate = MDY( InPut( Scan( OldDate , 2 , '/' ) , 8. ) ,
1 ,
InPut( Scan( OldDate , 1 , '/' ) , 8. ) ) ;
Put OldDate= NewDate= Date9. ;
Run
Toby Dunn
If anything simply cannot go wrong, it will anyway. Murphys Law #2.
The buddy system is essential to your survival; it gives the enemy somebody
else to shoot at.
Murphys Law #
Tell a man there are 300 billion stars in the universe and he'll believe
you. Tell him a bench has wet paint on it and he'll have to touch to be
sure. Murphys Law #9...