i have a sample dataset as below
subj | var2 | var3 | var4 |
1 | time | 12043 | mark |
1 | jack | 43535 | steve |
2 | bob | 52352 | vsvgv |
2 | steve | 24565 | fbhdhn |
3 | kdire | 2355 | rtgeff |
3 | dfsde | 63634 | rwege |
3 | kjyytt | 34235 |
fgsggsd |
and i need to set uplike below
subj | value | desc |
1 | time | var2 |
1 | 12043 | var3 |
1 | mark | var4 |
1 | jack | var2 |
1 | 43535 | var3 |
1 | steve | var4 |
2 | bob | var2 |
2 | 52352 | var3 |
2 | vsvgv | var4 |
2 | steve | var2 |
2 | 24565 | var3 |
2 | fbhdhn | var4 |
can anyone help me how to do this
Hello,
data have;
infile cards dlm=",";
input subj var2 $ var3 var4 $;
cards;
1,time,12043,mark
1,jack,43535,steve
2,bob,52352,vsvgv
2,steve,24565,fbhdhn
3,kdire,2355,rtgeff
3,dfsde,63634,rwege
3,kjyytt,34235,fgsggsd
;
run;
data want;
set have;
array nums(*) _NUMERIC_;
array chars(*) _CHARACTER_;
format value $10. desc $4.;
keep subj value desc;
/* We exclude subj fom the loop */
do i=2 to dim(nums);
value=strip(put(nums(i),10.));
desc=vname(nums(i));
output;
end;
do i=1 to dim(chars);
value=chars(i);
desc=vname(chars(i));
output;
end;
run;
Hello,
data have;
infile cards dlm=",";
input subj var2 $ var3 var4 $;
cards;
1,time,12043,mark
1,jack,43535,steve
2,bob,52352,vsvgv
2,steve,24565,fbhdhn
3,kdire,2355,rtgeff
3,dfsde,63634,rwege
3,kjyytt,34235,fgsggsd
;
run;
data want;
set have;
array nums(*) _NUMERIC_;
array chars(*) _CHARACTER_;
format value $10. desc $4.;
keep subj value desc;
/* We exclude subj fom the loop */
do i=2 to dim(nums);
value=strip(put(nums(i),10.));
desc=vname(nums(i));
output;
end;
do i=1 to dim(chars);
value=chars(i);
desc=vname(chars(i));
output;
end;
run;
Like this
data have;
input subj var2$ var3$ var4$;
datalines;
1 time 12043 mark
1 jack 43535 steve
2 bob 52352 vsvgv
2 steve 24565 fbhdhn
3 kdire 2355 rtgeff
3 dfsde 63634 rwege
3 kjyytt 34235 fgsggsd
;
data want;
set have;
array val{2:4} var2-var4;
do i=2 to 4;
value = val[i];
desc = vname(val[i]);
output;
end;
drop var2-var4 i;
run;
Out of interest, why? Neither of those two datasets you provide as example are of much use to anyone. Could just be the test data you provide, however there is no way from either of those to tell if 43535 should belong to time or jack for instance. I would suggest sorting out the data before getting to this stage, and assign proper ids and such like so that you can correctly identify a unique datapoint.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.