DATA Step, Macro, Functions and more

can i use between in if statement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 115
Accepted Solution

can i use between in if statement

Hi,

 

I want to create a new variable for which i have var1 as 1 to 20.

if var1 is 1 to 10 then create a new var as var2='first' and from 11 to 20 as var2='Last'.

 

Can i do this in datastep?


Accepted Solutions
Solution
‎03-30-2016 06:33 AM
Trusted Advisor
Posts: 1,115

Re: can i use between in if statement

Hi @vraj1,

 

I see. You "want to create a new variable" in an existing dataset "for which [you] have var1 as 1 to 20."

 

Whether you really need a new variable (with redundant information!) depends on what you plan to do with it. Maybe it's sufficient to create a format:

proc format;
value grp
 1-10 = 'first'
11-20 = 'last';
run;

You can do many things with your existing VAR1 together with this format. For example, perform a frequency count:

proc freq data=have;
format var1 grp.;
tables var1;
run;

If you really need the new variable, you can still create it using the format:

data want;
set have;
var2=put(var1, grp.);
run;

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: can i use between in if statement

Yes, that is no problem:

data want;
do var1=1 to 20;
var2=ifc(var1<=10,"First","Last");
output;
end;
run;

Not sure why you think you would not be able to do that in a datastep?

Frequent Contributor
Posts: 115

Re: can i use between in if statement

Yes, but this step is also doubling the obs.

I want like

 

id              var1    var2

101             1          first

102              2        first

103             19        last  

 

 

Super User
Posts: 6,930

Re: can i use between in if statement

No, it is not doubling the obs:

 

data have;
input id var1;
cards;
101 1
101 10
101 15
;
run;

title "Dataset HAVE";

proc print;
run;

data want;
set have;
var2=ifc(var1<=10,"First","Last");
run;

title "Dataset WANT";

proc print;
run;

gives this result:

                                                            Dataset HAVE

                                                         Obs     id    var1

                                                          1     101      1 
                                                          2     101     10 
                                                          3     101     15 
                                                            Dataset WANT

                                                    Obs     id    var1    var2

                                                     1     101      1     First
                                                     2     101     10     First
                                                     3     101     15     Last 

You see, there are the same number of observations in both datasets.

Maybe you need to be more specific with your requirements.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,392

Re: can i use between in if statement

Your requirement states, and I quote:

"I want to create a new variable for which i have var1 as 1 to 20."

Therefore your output dataset will have 20 observations, per my code.  If you requirements are not like, please clarify your problem, post test data - in the form of a datastep, and what the output should look like.

Solution
‎03-30-2016 06:33 AM
Trusted Advisor
Posts: 1,115

Re: can i use between in if statement

Hi @vraj1,

 

I see. You "want to create a new variable" in an existing dataset "for which [you] have var1 as 1 to 20."

 

Whether you really need a new variable (with redundant information!) depends on what you plan to do with it. Maybe it's sufficient to create a format:

proc format;
value grp
 1-10 = 'first'
11-20 = 'last';
run;

You can do many things with your existing VAR1 together with this format. For example, perform a frequency count:

proc freq data=have;
format var1 grp.;
tables var1;
run;

If you really need the new variable, you can still create it using the format:

data want;
set have;
var2=put(var1, grp.);
run;

 

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 266 views
  • 2 likes
  • 4 in conversation