DATA Step, Macro, Functions and more

data set up variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

data set up variables

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


Accepted Solutions
Solution
‎09-11-2017 04:13 AM
Regular Contributor
Posts: 240

Re: data set up variables

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


All Replies
Solution
‎09-11-2017 04:13 AM
Regular Contributor
Posts: 240

Re: data set up variables

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;
PROC Star
Posts: 768

Re: data set up variables

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;
Super User
Super User
Posts: 7,997

Re: data set up variables

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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