Create a new variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Create a new variable

I have a table with the variables: Contry, City and Client Name. I need to create a new column that give me a ID key but filter by contry and City.

Ex:

ID   Contry     City         Client Name     New Column

1     EN          LON        ....                   1

2     EN          LON        ....                   2     

3     EN          LIV          ....                    1

4     EN          LIV          ....                   2

5     ES          MAD       ....                     1

6     ES          MAD       ....                     2

7     ES          BCN       ....                     1

8     ES          BCN       ....                     2

9     PT          LIS         ....                    1

10    PT          LIS         ....                    2

11    PT          LIS         ....                     3

12    PT          OPO      ....                     1

I try with a data set, but return the same values from ID.

data query;

set tbl_client;

by icontry city;

new column + 1;

run;

Someone help me.

Cre


Accepted Solutions
Solution
‎10-01-2012 10:28 AM
Super Contributor
Posts: 1,636

Re: Create a new variable

data query;

set tbl_client;

by icontry city;

if first.city then new_column=0;

new_column + 1;

run;

View solution in original post


All Replies
Solution
‎10-01-2012 10:28 AM
Super Contributor
Posts: 1,636

Re: Create a new variable

data query;

set tbl_client;

by icontry city;

if first.city then new_column=0;

new_column + 1;

run;

Super User
Posts: 19,815

Re: Create a new variable

Close.

You have the by groups set up but need to use them now.  Add in the line below and you can see here for more details:

http://www.ats.ucla.edu/stat/sas/faq/enumerate.htm

data query;

set tbl_client;

by icontry city;

new_column + 1;

if first.city then new_column=1;

run;

Valued Guide
Posts: 765

Re: Create a new variable

hi ... another idea (same idea as resetting the counter to 0 for each group, but resets it differently) ...

data y;

set x;

by city notsorted;

new + 1 +(-first.city*new);

run;

Occasional Contributor
Posts: 5

Re: Create a new variable

Thank u all for solution

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 211 views
  • 0 likes
  • 4 in conversation