f



"If then; if then;" and "If then; if;"

I have a raw data set which is a hierarchical file:
H 321 s. main st
P Mary E    21 F
P william m 23 M
P Susan K    3 F
H 324 S. Main St
I use the folowing code to read the data to creat one observation per
detail(P) record including hearder record(H):
data test;
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
but the output is not what I want:
1     321 s. main     H
2     321 s. main     P      Mary E       21       F
3     321 s. main     P      william m    23       M
4     321 s. main     P      Susan K      3        F
5     324 S. Main     H
then I change the code to
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P';
input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
Then I got the right results:
1     321 s. main     P      Mary E       21       F
2     321 s. main     P      william m    23       M
3     321 s. main     P      Susan K      3        F
Can anyone tell me why "if...then input...; if...then input...;" has
different result from "if...then input...;if...;input...;"?
Tongjun
0
9/5/2005 12:48:29 PM
comp.soft-sys.sas 142828 articles. 1 followers. Post Follow

6 Replies
736 Views

Similar Articles

[PageSpeed] 29

Tongjun,

In your second example, you are using what is called a "subsetting IF
statement".  Such a statement causes a DATA step to continue processing
only those records which meet the condition of the expression.

If the expression is false, no further statements are processed for that
record, and the record isn't written to the data set.

Art
---------
On Mon, 5 Sep 2005 08:48:29 -0400, Tongjun Kang <mariannekang@YAHOO.COM>
wrote:

>I have a raw data set which is a hierarchical file:
>H 321 s. main st
>P Mary E    21 F
>P william m 23 M
>P Susan K    3 F
>H 324 S. Main St
>I use the folowing code to read the data to creat one observation per
>detail(P) record including hearder record(H):
>data test;
>infile 'C:\Documents and Settings\retain.txt';
>retain Address;
>input type $1. @;
>if type='H' then input @3 Address $12.;
>if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
>run;
>but the output is not what I want:
>1     321 s. main     H
>2     321 s. main     P      Mary E       21       F
>3     321 s. main     P      william m    23       M
>4     321 s. main     P      Susan K      3        F
>5     324 S. Main     H
>then I change the code to
>infile 'C:\Documents and Settings\retain.txt';
>retain Address;
>input type $1. @;
>if type='H' then input @3 Address $12.;
>if type='P';
>input @3 Name $10. @13 Age 3. @16 Gender $1.;
>run;
>Then I got the right results:
>1     321 s. main     P      Mary E       21       F
>2     321 s. main     P      william m    23       M
>3     321 s. main     P      Susan K      3        F
>Can anyone tell me why "if...then input...; if...then input...;" has
>different result from "if...then input...;if...;input...;"?
>Tongjun
0
art297 (4213)
9/5/2005 1:19:57 PM
Hi,

What if... ; input ...;does is it puts a restriction on the database to output
only those observations for which this if is satisfied.The input statement
following it has nothing to do with the if and will be run for those
observations for which the if evaluated to true. The observations for which if
was not true wont be output at all.



cheers,
mohit




                      "Tongjun Kang"
                      <mariannekang@YAH        To:       SAS-L@LISTSERV.UGA.EDU
                      OO.COM>                  cc:
                      Sent by: "SAS(r)         Subject:  "If then; if then;" and "If then; if;"
                      Discussion"
                      <SAS-L@LISTSERV.U
                      GA.EDU>


                      09/05/2005 06:18
                      PM
                      Please respond to
                      "Tongjun Kang"






I have a raw data set which is a hierarchical file:
H 321 s. main st
P Mary E    21 F
P william m 23 M
P Susan K    3 F
H 324 S. Main St
I use the folowing code to read the data to creat one observation per
detail(P) record including hearder record(H):
data test;
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
but the output is not what I want:
1     321 s. main     H
2     321 s. main     P      Mary E       21       F
3     321 s. main     P      william m    23       M
4     321 s. main     P      Susan K      3        F
5     324 S. Main     H
then I change the code to
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P';
input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
Then I got the right results:
1     321 s. main     P      Mary E       21       F
2     321 s. main     P      william m    23       M
3     321 s. main     P      Susan K      3        F
Can anyone tell me why "if...then input...; if...then input...;" has
different result from "if...then input...;if...;input...;"?
Tongjun







American Express made the following
 annotations on 09/05/05, 07:06:10
------------------------------------------------------------------------------
******************************************************************************

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."

******************************************************************************
==============================================================================
0
9/5/2005 1:24:20 PM
Tongjun:
SAS defines the form of IF statement, "if type=3D'P';", a 'subsetting =
IF". It filters all rows out of output except for those of type 'P'. The =
RETAIN statement carries the address read from the prior row of input by =
the "IF type=3D'H' THEN ...;" statement.
Sig

________________________________

From: owner-sas-l@listserv.uga.edu on behalf of Tongjun Kang
Sent: Mon 9/5/2005 8:48 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: "If then; if then;" and "If then; if;"



I have a raw data set which is a hierarchical file:
H 321 s. main st
P Mary E    21 F
P william m 23 M
P Susan K    3 F
H 324 S. Main St
I use the folowing code to read the data to creat one observation per
detail(P) record including hearder record(H):
data test;
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type=3D'H' then input @3 Address $12.;
if type=3D'P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
but the output is not what I want:
1     321 s. main     H
2     321 s. main     P      Mary E       21       F
3     321 s. main     P      william m    23       M
4     321 s. main     P      Susan K      3        F
5     324 S. Main     H
then I change the code to
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type=3D'H' then input @3 Address $12.;
if type=3D'P';
input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
Then I got the right results:
1     321 s. main     P      Mary E       21       F
2     321 s. main     P      william m    23       M
3     321 s. main     P      Susan K      3        F
Can anyone tell me why "if...then input...; if...then input...;" has
different result from "if...then input...;if...;input...;"?
Tongjun
0
HERMANS1 (2684)
9/5/2005 1:32:29 PM
There is an implied OUTPUT at the bottom of your loop.

In your first code:
        You read the first card - type is set to H.
        The first if is true so you read address from this card.
        The second if is false, you skip the input statement.
        You output the partial record.
        You read the second card - type is set to P.
        The first if is false.
        The second if is true so you read name etc from this card.
        You output the complete record.
        You read the third card...

In your second code:
        You read the first card - type is set to H.
        The first if is true so you read address from this card.
        The second (subsetting) if is false SO YOU SKIP THE REMAINDER OF
THE DATA STEP LOOP.
        You read the second card ...

You could have made you first code work by changing the second if to:
        if type='P' then do;
                input @3 Name $10. @13 Age 3. @16 Gender $1.;
                output;
        end;

The presence of the explicit output statement removes the implied one at
the bottom of the data step.  Output will only occur while you within
the range of the second if.

Barry Schwarz
OS/390 System Programmer
M/S 80-JE
Phone: 253-773-4221
Fax: 253-773-1257


-----Original Message-----
From: Tongjun Kang [mailto:mariannekang@YAHOO.COM]
Sent: Monday, September 05, 2005 5:48 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: "If then; if then;" and "If then; if;"


I have a raw data set which is a hierarchical file:
H 321 s. main st
P Mary E    21 F
P william m 23 M
P Susan K    3 F
H 324 S. Main St
I use the folowing code to read the data to creat one observation per
detail(P) record including hearder record(H):
data test;
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.; run; but
the output is not what I want:
1     321 s. main     H
2     321 s. main     P      Mary E       21       F
3     321 s. main     P      william m    23       M
4     321 s. main     P      Susan K      3        F
5     324 S. Main     H
then I change the code to
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P';
input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
Then I got the right results:
1     321 s. main     P      Mary E       21       F
2     321 s. main     P      william m    23       M
3     321 s. main     P      Susan K      3        F
Can anyone tell me why "if...then input...; if...then input...;" has
different result from "if...then input...;if...;input...;"? Tongjun
0
9/6/2005 6:15:46 PM
Tongjun,

You must know the default data step actions for ordinary if-then-else
statement and
SPECIAL Subsetting If statement.

[1]  In case of the former, the contents of PDF is Output when it
reaches the end of
the data step for either of the IF and ELSE conditions whichever is
true that time.

[2]  In case of SUBSETTING IF, the contents of PDF is not OUTPUT until
the
subsetting if condition becomes TRUE.  For instance, when type = H ,
the contents have
the address and when it comes to the next line and checks the
SUBSETTING IF
condition which is not TRUE. The implicit ELSE condition acts now -
no output of
PDF is written and control is directed to the TOP of the data step. At
this time the
_N_ is assigned the next record-number to read in. Now the type=P is
read, and the
SUSETTING IF condition is found to be TRUE and the implicit THEN
condition acts
now  - output of PDF is written out and control is directed to the TOP
of data step.

HTH.

Muthia Kachirayan

----------------------------------------------------------------------------------
I have a raw data set which is a hierarchical file:
H 321 s. main st
P Mary E    21 F
P william m 23 M
P Susan K    3 F
H 324 S. Main St
I use the folowing code to read the data to creat one observation per
detail(P) record including hearder record(H):
data test;
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
but the output is not what I want:
1     321 s. main     H
2     321 s. main     P      Mary E       21       F
3     321 s. main     P      william m    23       M
4     321 s. main     P      Susan K      3        F
5     324 S. Main     H
then I change the code to
infile 'C:\Documents and Settings\retain.txt';
retain Address;
input type $1. @;
if type='H' then input @3 Address $12.;
if type='P';
input @3 Name $10. @13 Age 3. @16 Gender $1.;
run;
Then I got the right results:
1     321 s. main     P      Mary E       21       F
2     321 s. main     P      william m    23       M
3     321 s. main     P      Susan K      3        F
Can anyone tell me why "if...then input...; if...then input...;" has
different result from "if...then input...;if...;input...;"? 
Tongjun

0
mkachi (27)
9/9/2005 2:15:13 PM
Tongjun,

if "if type = 'P';" is a subsetting if (selection) condition. If the
expression in after "if" -- in this cas, "type = 'P'" -- returns false,
then the program goes on to process the next input record without
writing anything to the SAS data table. SAS outputs an observation to
the data set only if the selection condition is met. On the other hand,
if the same expression is part of an "if <> then" statement, then, in
case that the "if" fails, the only consequence is that the "then" does
not get executed. In the "if <> then" case an observation is always
written to the SAS table. The "@" at the end of "input type $1. @;" has
no effect in this case.

Also, because you are using "retain address;", the address information
is always kept for the next iteration of the data step. It changes
(gets overwritten by another address) only if "if type = 'H'" returns
true.

You may want to read more about program data vector and the various
versions of the "if" statement in the SAS manuals. These are also
available online at
http://v8doc.sas.com/sashtml/ (if you, like me, are stuck with using
version 8)
http://support.sas.com/documentation/onlinedoc/sas9doc.html (for
Version 9)

Best regards,

Janis Jekabsons

Tongjun Kang wrote:
> I have a raw data set which is a hierarchical file:
> H 321 s. main st
> P Mary E    21 F
> P william m 23 M
> P Susan K    3 F
> H 324 S. Main St
> I use the folowing code to read the data to creat one observation per
> detail(P) record including hearder record(H):
> data test;
> infile 'C:\Documents and Settings\retain.txt';
> retain Address;
> input type $1. @;
> if type='H' then input @3 Address $12.;
> if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.;
> run;
> but the output is not what I want:
> 1     321 s. main     H
> 2     321 s. main     P      Mary E       21       F
> 3     321 s. main     P      william m    23       M
> 4     321 s. main     P      Susan K      3        F
> 5     324 S. Main     H
> then I change the code to
> infile 'C:\Documents and Settings\retain.txt';
> retain Address;
> input type $1. @;
> if type='H' then input @3 Address $12.;
> if type='P';
> input @3 Name $10. @13 Age 3. @16 Gender $1.;
> run;
> Then I got the right results:
> 1     321 s. main     P      Mary E       21       F
> 2     321 s. main     P      william m    23       M
> 3     321 s. main     P      Susan K      3        F
> Can anyone tell me why "if...then input...; if...then input...;" has
> different result from "if...then input...;if...;input...;"?
> Tongjun

0
jekabsoj (5)
9/9/2005 2:32:14 PM
Reply:

Similar Artilces:

""""""""""""""""""""""ADD ME""""""""""""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfo Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... We look forward to build a ve...

"""""""""ADD ME""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfoi Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... ...

Urgent Requirement in """""""""""""NEW YORK""""""""""""""""
Hello Partners, Please find the requirement below. Please send the updated resume along with rate and contact no. REQ#1: Title : Java Developer ( Rating Project) Duration : 6 months Rate : open Location : NY strong java, WebLogic 9.2, Web Services, Oracle REQ#2: Title : Java Developer Duration : 4 months Rate : open Location : NY Strong java, SQL REQ#3: Title : VB.Net Consultant Location : NY Duration : 4 months Rate : open Primarily looking at someone who has Excel, VB.net a...

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

"or" and "and"
Hi, I'm just getting to discover ruby, but I find it very nice programming language. I just still don't understand how the "or" and "and" in ruby... I was playing with ruby and for example made a def to print Stem and Leaf plot (for those who didn't have a statistics course or slept on it, e.g. http://cnx.org/content/m10157/latest/) Here is the Beta version of it: class Array def n ; self.size ; end def stem_and_leaf(st = 1) # if st != (2 or 5 or 10) then ; st = 1 ; end k = Hash.new(0) self.each {|x| k[x.to_f] += 1 } k = k.sort{|a, b| a[0].to_f <=&g...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

"my" and "our"
Hi, while testing a program, I erroneously declared the same variable twice within a block, the first time with "my", the second time with "our": { my $fz = 'VTX_Link'; .... ( around 200 lines of code, all in the same block) our $fz = 'VTX_Linkset'; ... } So the initial contents of the $fz declared with "my" is lost, because "our" creates a lexical alias for the global $fz, thus overwriting the previous "my" declaration. It was my error, no question. But I wonder why Perl doesn't mention this - even with "use s...

why "::", not "."
Why does the method of modules use a dot, and the constants a double colon? e.g. Math::PI and Math.cos -- Posted via http://www.ruby-forum.com/. On Oct 26, 2010, at 01:48 , Oleg Igor wrote: > Why does the method of modules use a dot, and the constants a double > colon? > e.g. > Math::PI and Math.cos For the same reason why inner-classes/modules use double colon, because = they're constants and that's how you look up via constant namespace. Math::PI and ActiveRecord::Base are the same type of lookup... it is = just that Base is a module and PI is a float....

"/a" is not "/a" ?
Hi everybody, while testing a module today I stumbled on something that I can work around but I don't quite understand. >>> a = "a" >>> b = "a" >>> a == b True >>> a is b True >>> c = "/a" >>> d = "/a" >>> c == d True # all good so far >>> c is d False # eeeeek! Why c and d point to two different objects with an identical string content rather than the same object? Manu Emanuele D'Arrigo wrote: >>>> c = "/a" >>>&...

Urgent Requirement for """""""""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting ...

Urgent need """""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting Database/ETL requirements (Optional) ...

Does it need a ";" at the very after of "if" and "for"
write code like: int main(void) { int a=10; if(a<20) {} } Compiler ok on dev-cpp . don't we have to add a ";" after if statement? marsarden said: > write code like: > > int main(void) > { > int a=10; > if(a<20) > {} > } > > Compiler ok on dev-cpp . don't we have to add a ";" after if > statement? The syntax for 'if' is: if(expression) statement There is no semicolon after the ) but before the statement. The statement is either a normal statement (which can be empty), ending in a semicolon:- if(expr) ...

"In" "Out" and "Trash"
I just bought a new computer and I re-installed Eudora Light on my new computer. But when I open Eudora, the "In", "Out" and "Trash" links are not on the left side of the screen the way they were on my old computer. How can I get these links back on the left side of the screen? Thank you. On 25 Mar 2007 09:49:22 -0700, "abx" <abfunex@yahoo.com> wrote: >I just bought a new computer and I re-installed Eudora Light on my new >computer. But when I open Eudora, the "In", "Out" and "Trash" links >are ...

A problem about "[ ]" "( )" "="
I want to read several images saved in a director,and give them to I1,I2 ,I3....,using the following codes: filelist=dir(['c:\MATLAB701\work\...\*.jpg']); for i=1 :length(filelist) I=imread(fullfile('c:\MATLAB701\work\...',filelist(i).name)); end; but failed. Then I used I(i)=imread... ,still failed. How could I do? "John" <mailofww@126.com> wrote in message news:ef19e12.-1@webx.raydaftYaTP... >I want to read several images saved in a director,and give them to > I1,I2 ,I3....,using the following codes: > filelist=dir(['c:\MATLAB701\work\.....

Web resources about - "If then; if then;" and "If then; if;" - comp.soft-sys.sas

Resources last updated: 1/28/2016 9:17:57 PM