Hi All,
I want to copy the variable name from the last group to all the other observations in the same group. For example, copying the last variable name (Country) in Category to all the blank space in the category.
I have a dataset:
Category | Country | Group |
US | 1 | |
Israel | 1 | |
Canada | 1 | |
Country | Australia | 1 |
| US | 2 |
Israel | 2 | |
Canada | 2 | |
Country | Australia | 2 |
I want:
Category | Country | Group |
Country | US | 1 |
Country | Israel | 1 |
Country | Canada | 1 |
Country | Australia | 1 |
Country | US | 2 |
Country | Israel | 2 |
Country | Canada | 2 |
Country | Australia | 2 |
The code for the dataset I have:
data have;
infile datalines delimiter=',';
input Category $ Country $ group$;
datalines;
,USA , 1
,ISRAEL ,1
,CANADA, 1
Country, AUSTRALIA ,1
,USA, 2
,ISRAEL ,2
,CANADA, 2
Country, AUSTRALIA ,2
;
run;
Thanks.
Best,
Sandyzman1
data want;
if 0 then set have; /* sets length for all variables */
do until (category > "");
set have;
end;
do until (c > "");
set have (rename=(category=c));
output;
end;
drop c;
run;
I seem to be doing a lot of double DO loops lately ...
In this trivial example, you just need to put the text "Country" in the Category column, there's no copying needed. Is it really a more complicated problem? If so, can you give us a more realistic example?
I am writing a macro to create a table for the univariable logistic regression. I was able to output the class variable which has only the last category. However, to merge the class table into the OR table using BY statement, I need to copy the last variable name to the rest of the empty cell by the group.
I just wanted to simplify the issue, so I created the table such that there is an empty space. Thanks.
Sandyzman1
data want;
if 0 then set have; /* sets length for all variables */
do until (category > "");
set have;
end;
do until (c > "");
set have (rename=(category=c));
output;
end;
drop c;
run;
I seem to be doing a lot of double DO loops lately ...
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: