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

Hi,

I have 2 variables var1 and var2, Var1 is a grouping variable and var2 is reference variable. Here i want to create new variable called Required which takes values from Var2 from the group(var1) and should copy to new variable (Required).

I tried with first. and last. My logic is not working for this data. as u can see then values may lie middle of the group.

And in the third group you can see we have 2 values 90696x22 and GWYSGNx1afdMxcVGQb2x19, in this case i want to GWYSGNx1afdMxcVGQb2x19 to new variable. Hint is i want to copy only those values from var2 who's value is ending with "x19".

It will be great full for your support.

Thanks

Var 1Var 2Required
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19Hmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19Hmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
0000000000000000001800008640326AHmas$ZYcNhrbd30NniiOc6x19
00033F83EF8F58FB6AF3ACA6FFFFFFFFOmrsjLWkNxioc7M1Oj6$bex19OmrsjLWkNxioc7M1Oj6$bex19
00033F83EF8F58FB6AF3ACA6FFFFFFFFOmrsjLWkNxioc7M1Oj6$bex19
00033F83EF8F58FB6AF3ACA6FFFFFFFFOmrsjLWkNxioc7M1Oj6$bex19
00033F83EF8F58FB6AF3ACA6FFFFFFFFOmrsjLWkNxioc7M1Oj6$bex19
00033F83EF8F58FB6AF3ACA6FFFFFFFFOmrsjLWkNxioc7M1Oj6$bex19
001EE2E43C3711D1A0193A08FFFFFFFF90696x22EU$GWYSGNx1afdMxcVGQb2x19
001EE2E43C3711D1A0193A08FFFFFFFFEU$GWYSGNx1afdMxcVGQb2x19
001EE2E43C3711D1A0193A08FFFFFFFF90696x22EU$GWYSGNx1afdMxcVGQb2x19
001EE2E43C3711D1A0193A08FFFFFFFFEU$GWYSGNx1afdMxcVGQb2x19
001EE2E43C3711D1A0193A08FFFFFFFFEU$GWYSGNx1afdMxcVGQb2x19
001EE2E43C3711D1A0193A08FFFFFFFFEU$GWYSGNx1afdMxcVGQb2x19EU$GWYSGNx1afdMxcVGQb2x19
9FB83E1BFC68D743250F4D9AFFFFFFFF05qOOZmsNxha0cM1Oj6$bex19
9FB83E1BFC68D743250F4D9AFFFFFFFF05qOOZmsNxha0cM1Oj6$bex19
9FB83E1BFC68D743250F4D9AFFFFFFFF05qOOZmsNxha0cM1Oj6$bex19
9FB83E1BFC68D743250F4D9AFFFFFFFF05qOOZmsNxha0cM1Oj6$bex19
9FB83E1BFC68D743250F4D9AFFFFFFFF05qOOZmsNxha0cM1Oj6$bex19
A0012E3CA47467275AD0EC12FFFFFFFF05qOOZmsNxha0cM1Oj6$bex1905qOOZmsNxha0cM1Oj6$bex19
1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

Hi SasPro.

I wrote a simple code, but the results are slightly different.

*sort original data by var1 and var2;

proc sort data = have;by var1 var2;run;

*create a temp file that flags the observations that have the "19" in your var2;

data temp(keep = var1 var2 rename = var2 = required);

    set have;

    by var1 var2;

    flag = ((scan(left(var2),-1,"x")) = "19" );

    if flag = 1;

    if first.var2;

run;

*merge it back together;

data want;

    merge have temp;

    by var1;

run;

As you can see, my final data set doesn't copy var2 info for the last record"A0012E3CA47467275AD0EC12FFFFFFFF" into the second to last record "A0012E3CA47467275AD0EC12FFFFFFFF"

(Not sure how you decide that the info from the last record should be populated into the second to last?)

Good luck.

Anca.

View solution in original post

2 REPLIES 2
AncaTilea
Pyrite | Level 9

Hi SasPro.

I wrote a simple code, but the results are slightly different.

*sort original data by var1 and var2;

proc sort data = have;by var1 var2;run;

*create a temp file that flags the observations that have the "19" in your var2;

data temp(keep = var1 var2 rename = var2 = required);

    set have;

    by var1 var2;

    flag = ((scan(left(var2),-1,"x")) = "19" );

    if flag = 1;

    if first.var2;

run;

*merge it back together;

data want;

    merge have temp;

    by var1;

run;

As you can see, my final data set doesn't copy var2 info for the last record"A0012E3CA47467275AD0EC12FFFFFFFF" into the second to last record "A0012E3CA47467275AD0EC12FFFFFFFF"

(Not sure how you decide that the info from the last record should be populated into the second to last?)

Good luck.

Anca.

SasPro
Calcite | Level 5

Thank you Anca telia,

Your code is working perfect! Smiley Happy

I tried to execute with other logic it is also working file Smiley Happy

proc sort data = have;by var1 descending

var2;run;

data temp;

set have;

by var1 ;

if first.var1 then output;

Run;

data want;

    merge have temp;

    by var1;

run;

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
  • 2 replies
  • 2181 views
  • 0 likes
  • 2 in conversation