- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could use ARRAYs. Then you need only one if-then statement. But since you give us no information about your data set variable names, we can't provide any more details.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Depends.. Can you be a bit more precise about the nature of your problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use an array to perform the same operation on multiple variables:
Data want;
Set have;
Array _arr(*) var1 var2 var3 ;
Do i=1 to dim(_arr);
If _arr(i)=0 then _arr(i)=2;
End;
Drop i;
Run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It overwrites the same variable. Is there a way to create new variable (which is recoded) in array statement
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@knargis160 wrote:
It overwrites the same variable. Is there a way to create new variable (which is recoded) in array statement
Thanks!
Could you please explain the whole problem in sufficient detail (and by showing an example data set and by showing the desired output) that we can understand it? Then we might be able to provide code that meets your needs.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Datalines
ID P1 P2 P3 P4 ….
1 0 1 0 1
2 1 0 0 0
3 0 1 1 1
Output
ID P1 P2 P3 P4
1 2 1 2 1
2 1 2 2 2
3 2 1 1 1
I want to code all "0" to 2
As these are 15 vars like this. I don't want to use if multiple else statement
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your output shows as it overwriting the original values, but you said that's not what you want above so please clarify.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
HI @knargis160
You can do this to recode variables in new variables:
data want;
set have;
array _arr(*) var1 var2 var3 /* Put the variable names */;
array _new(3) ; /* Put the number of variables */
do i=1 to dim(_arr);
if _arr(i)=0 then _arr(i)=2;
end;
drop i;
run;
Best,
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@knargis160 wrote:
I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?
Another approach can be when you read the data from an external source. (informat and example data changed from first response)
proc format library=work; invalue changetwo '0'=2 other=_same_ ; run; data example; informat x y z changetwo.; input x y z; datalines; 1 0 2 0 0 0 1 1 0;
A question though. Are you changing 0 to 2 because you have a dichotomous variable that has 1 for 'Yes' 'True' 'Present' and 0 for 'No' 'False' 'Absent' or similar? You may want to strongly consider not doing so. It can require more code to get somethings done with 1/2 coding than with 1/0 coding.
Consider: if you SUM a 1/0 coded variable then the total is the number of observations or variables that were coded 1.
The MEAN is the percentage of 1 values in decimal form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Method 1: Arrays
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
If you need further help please post example data and show what you want as output.
Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
@knargis160 wrote:
I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?