Hello,
I have numeric variables and characters that start with x.
I want to create two vectors as follows, but I can not.
data tab1;
input id x1 x2 x3 x4 x5 x6 $ X7 $;
cards;
1 3 2 2 . 1 a b
1 . 1 1 3 2 . b
1 9 . 3 9 6 . b
1 8 . 7 3 2 a b
1 8 2 . 6 . a b
1 6 5 . . 9 a .
2 1 3 . 8 . . b
2 2 5 8 . . a b
2 3 3 11 . . a b
2 4 3 9 . 10 a b
;
run;
data tab2;
set tab1;
array Xnum(*) x: _numeric_;
array Xchar(*) x: _CHARACTER_;
/*Instructions*/
run;
Is there a possibility to do this in sas?
example:
use these options as functions
_numeric_ (x: )
_character_ (y: )
Thank you
Here's a variation that doesn't require you to sort through which are numeric and which are character.
array nums {*} x1-numeric-x7;
array chars {*} x1-character-x7;
Just don't specify the x: part:
data tab1; input id x1 x2 x3 x4 x5 x6 $ X7 $;cards;1 3 2 2 . 1 a b1 . 1 1 3 2 . b1 9 . 3 9 6 . b1 8 . 7 3 2 a b1 8 2 . 6 . a b1 6 5 . . 9 a .2 1 3 . 8 . . b2 2 5 8 . . a b2 3 3 11 . . a b2 4 3 9 . 10 a b; run; data tab2; set tab1; array xnum(*) _numeric_; array xchar(*) _character_; /*Instructions*/ run;
True you will need to do a check to see if vname="id" and not do your logic for that one (as id is also numeric).
Otherwise you will need to build lists of variables from sashelp.vcolumn, which is harder.
Use from-to variable lists:
data tab1;
input id x1 x2 x3 x4 x5 x6 $ X7 $;
cards;
1 3 2 2 . 1 a b
1 . 1 1 3 2 . b
1 9 . 3 9 6 . b
1 8 . 7 3 2 a b
1 8 2 . 6 . a b
1 6 5 . . 9 a .
2 1 3 . 8 . . b
2 2 5 8 . . a b
2 3 3 11 . . a b
2 4 3 9 . 10 a b
;
run;
data tab2;
set tab1;
array Xnum(*) x1-x5;
array Xchar(*) x6-x7;
/*Instructions*/
run;
Here's a variation that doesn't require you to sort through which are numeric and which are character.
array nums {*} x1-numeric-x7;
array chars {*} x1-character-x7;
data tab2;
set tab1;
array Xnum(*) x1-numeric-x7;
array Xchar(*) x1-character-x7;
run;
thank you
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.