I have two variables MSW and MSWO in a dataset New. Both varaibles have 0 and 1 as data points.
If I use proc freq;
Proc freq data =new;
tables MSW MSWO;
run;
MSW have 1500 one's
MSWO have 2000 one's.
Now I want to create a new variable say DET which will 500 one's that is 2000-1500.
Not a very concise description. You really should cast the problem in terms of the existing values per record other wise there is no way for us to know the "right" one is assigned.
If you want a "one" when both of your variables are one then:
data want; set have; new = (msw=1 and mswo=1); run;
should do it. But if there is more particular rule as to when a 1 is created then you need to state that.
Other wise:
data junk; set have; newvar = (_n_ le 500); run;
will create a 1 value for the first 500 rows. Which meets your requirement but likely not what you actually want.
Better would be to provide an example of your existing data with combinations of the two variables and indicate which ones should get a 1 result. Since you have two variables with 2 values each that should only require 4 rows of data unless there are yet to be named or shown variables that actually play a role in the process.
At this point, nobody can tell which 500 observations you want. In fact, we can't even tell if there are many more than 500 observations that would meet the criteria you are setting up. Here's a program that will start you in the right direction:
proc freq data =new;
tables MSW * MSWO / list missing;
run;
The basic problem is that we can't tell how many observations overlap (both MSW and MSWO = 1). In fact, it is possible that none of the observations overlap in that way. So this program will continue your journey of learning about your data so you can make an informed decision about how to proceed.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.