SAS conditional input statements

Reply
Contributor
Posts: 69

SAS conditional input statements

[ Edited ]

HI There,

 

I have a sample data , where I want to read input be seeing the record number.

Like below:

data x;
infile filein19 missover dsd firstobs=1 dlm=',';
array srcs {2} $17. src1-src2;
array amts{2} amount1-amount2;
informat amount1-amount2 comma17.;
retain src1-src2;

if _n_ = 1 then;
input
skip1
(src1-src2) (:$17.)
;

if (1 < _n_ <= 5) then;
input
ssn : $11.
(amount1-amount2) (: 17.)
;

 

for the 1st condition (_n_=1) it is reading correctly but while coming next condition it is not reading correctly . It is just reading the 2nd obs and rest obs like from 3 to 5 are not appearing in final output. 

Please help.

Super User
Posts: 10,278

Re: SAS conditional input statements

Posted in reply to suchismita

A statement like that

if _n_ = 1 then;

does exactly nothing, as the whole if/then ends with the semicolon. Either have one statement before the semicolon, or add a do/end block.

All your other statements are executed in all data step iterations, unconditionally.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 69

Re: SAS conditional input statements

Posted in reply to KurtBremser

@KurtBremser i did it with do ...end as well. But it still reads only 1st record for if (2 _n_ <=5)  condition.

 

for an example, let's say my data is like: (it is .csv file)

id       shop1          shop2           Total
136                         207.72        207.72
139      979.38                         979.38
140     19,331.73                    19,331.73
157     13,300.96                    13,300.96

Totals   33,612.07   207.72      33,819.79

 

Now i want read it in such a way that my dataset will look like

id         source      amount    

136      shop2       207.72

139     shop1       979.38

140     shop1      19,331.73

157      shop1        13,300.96

 

Now my code is like

data x (keep=id source amount);
infile filein19 missover dsd firstobs=1 dlm=',';
array srcs {2} $17. src1-src2;
array amts{2} amount1-amount2;
informat amount1-amount2 comma17.;
retain src1-src2;
if _n_ = 1 then do;
input
s1 $
(src1-src2) (:$17.)
;
end;

if (1 < _n_ <= 5) then do;
input
id : 3.
(amount1-amount2) (: 17.)
;

do i= 1 to 2;
source = srcs{i};
amount = amts{i};
end;
end;

 

But it is just reading  shop2 as source and for amount column only 207.72 is there . Other amounts are missing. The source shop1 also not getting read.

 

Please suggest what is wrong here.

Super User
Posts: 10,278

Re: SAS conditional input statements

Posted in reply to suchismita

Post the csv file in a {i} window or as attachment, please.

After this loop

do i= 1 to 2;
source = srcs{i};
amount = amts{i};
end;

source and amount will always have the values of the second array elements.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 69

Re: SAS conditional input statements

Posted in reply to KurtBremser

Here you go with the csv file.

 

Super User
Posts: 23,771

Re: SAS conditional input statements

Posted in reply to suchismita

Assuming this is simplified and you have multiple SHOPS beyond the two here's a dynamic approach. 

If you only have two columns (shop1/shop2) a few IF/THEN statements is much quicker and simpler to code.

 

filename filein19 '/folders/myfolders/Book1.csv';

data x;
    infile filein19 dlm=',' dsd firstobs=2 truncover;
    informat id $8. _shop1 _shop2 _totals $12.;
    input ID _shop1 _shop2 _totals;
    array _orig(*) $ _shop1-_shop2;
    array _new(*) shop1-shop2;

    if ID in ('Totals', '') then
        delete;
    *convert character to numerics;

    do i=1 to dim(_orig);

        if not missing(_orig(i)) then
            _new(i)=input(_orig(i), comma32.);
    end;

   
    shop=coalesce(of _new(*));
    shop_index=whichn(shop, of _new(*));

    keep ID shop shop_index;
run;
Contributor
Posts: 69

Re: SAS conditional input statements

@Reeza thanks. I am new to SAS programming. Could you please explain me the logic that you are using. 

 

Super User
Posts: 10,278

Re: SAS conditional input statements

Posted in reply to suchismita

Study the documentation on the relevant components of the SAS language:

of variable_list allows simple naming of a group of variables in functions that accept an arbitrary number of arguments.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 69

Re: SAS conditional input statements

Posted in reply to suchismita
i was able to solve the issue. Thanks for the help.
Ask a Question
Discussion stats
  • 8 replies
  • 169 views
  • 0 likes
  • 3 in conversation