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

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:

CategoryCountryGroup 
 US1
 Israel1
 Canada1
CountryAustralia1

 

US2
 Israel2
 Canada2
CountryAustralia

2

 

I want: 

CategoryCountryGroup 
CountryUS1
CountryIsrael1
CountryCanada1
CountryAustralia1

Country

US2
CountryIsrael2
CountryCanada2
CountryAustralia2

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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 ...

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
sandyzman1
Obsidian | Level 7

@PaigeMiller 

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

Kurt_Bremser
Super User
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 ...

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 4 replies
  • 428 views
  • 0 likes
  • 3 in conversation