BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vraj1
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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;

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

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;
PeterClemmensen
Tourmaline | Level 20

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 763 views
  • 1 like
  • 4 in conversation